I want to load variables in a loop with different imagenames.
for i=1:length(imagefile)
name=imagefile{i};
% name=image01% load name
end
it looks variable (name) and not (image01), how should I do it
regards,
Not sure exactly what your variable is. An array of strings?
imaefgile = ["image01", "image02"]
for i=1:length(imaefgile)
load(imaefgile(i))
end
P.S. you may also need something like:
load(strcat("Folder/", imaefgile(i), ".mat"))
to concatenate the filename appropriately.
Related
I want to achieve this:
*.numPairs = ${N=3}
**.host[0..N].udpApp[0].typename = "UdpBasicApp"
But it does not work. However, if I do like this: **.host[0..3].udpApp[0].typename = "UdpBasicApp", then it works properly.
It means that the problem is that the variable N inside host[] (i.e., host[0..N]) does not work.
Can anyone suggest how to fix this issue (i.e., how to make a variable work inside host[])? Many thanks.
One cannot use a variable as an index of module.
Is it possible to change the name of a file for each iteration of a loop in Julia, with writedlm command? For example, with the following code, at each iteration, the file name is overwritten each time and I want avoid that, preferring different names for each iteration using each index i for that:
using DelimitedFiles
for i=1:100
data=rand(50,1000)
writedlm("candidates.txt", data, " ")
end
How can I achieve that?
Just write e.g.:
writedlm("candidates_$i.txt", data, " ")
This syntax is using interpolation, see here for details.
(I assume this is what you needed)
I am stuck with a Requirement but stuck in a Scenario, tried out lot of alternatives from Google but looks like not serving my Purpose.
Below is my Requirement-
tablename=$1
This is a Variable for which the value i get from the command line.
Now my requirement is i want to create a variable with the Name which i received in the variable called tablename.
And i want to use that variable in a loop further.
For Example :
Say,
tablename = dept1
Now my loop will look something like-
for dept1 in .... do
...
done
Can someone please help me with this..
My goal is to import a one column of a CSV file into a Ruby array. This is for a self-contained Ruby script, not an application. I'll just be running the script in Terminal and getting an output.
I'm having trouble finding the best way to import the file and finding the best way to dynamically insert the name of the file into that line of code. The filename will be different each time, and will be passed in by the user. I'm using $stdin.gets.chomp to ask the user for the filename, and setting it equal to file_name.
Can someone help me with this? Here's what I have for this part of the script:
require 'csv'
zip_array = CSV.read("path/to/file_name.csv")
and I need to be able to insert the proper file path above. Is this correct? And how do I get that path name in there? Maybe I'll need to totally re-structure my script, but any suggestions on how to do this?
There are two questions here, I think. The first is about getting user input from the command line. The usual way to do this is with ARGV. In your program you could do file_name = ARGV[0] so a user could type ruby your_program.rb path/to/file_name.csv on the command line.
The next is about reading CSVs. Using CSV.read will take the whole CSV, not just a single column. If you want to choose one column of many, you are likely better off doing:
zip_array = []
CSV.foreach(file_name) { |row| zip_array << row[whichever_column] }
Okay, first problem:
a) The file name will be different on each run (I'm supposing it will always be a CSV file, right?)
You can solve this problem with creating a folder, say input_data inside your Ruby script. Then do:
Dir.glob('input_data/*.csv')
This will produce an array of ALL files inside that folder that end with CSV. If we assume there will be only 1 file at a time in that folder (with a different name), we can do:
file_name = Dir.glob('input_data/*.csv')[0]
This way you'll dynamically get the file path, no matter what the file is named. If the csv file is inside the same directory as your Ruby script, you can just do:
Dir.glob('*.csv')[0]
Now, for importing only 1 column into a Ruby array (let's suppose it's the first column):
require 'csv'
array = []
CSV.foreach(file_name) do |csv_row|
array << csv_row[0] # [0] for the first column, [1] for the second etc.
end
What if your CSV file has headers? Suppose your column name is 'Total'. You can do:
require 'csv'
array = []
CSV.foreach(file_name, headers: true) do |csv_row|
array << csv_row['Total']
end
Now it doesn't matter if your column is the 1st column, the 3rd etc, as long as it has a header named 'Total', Ruby will find it.
CSV.foreach reads your file line-by-line and is good for big files. CSV.read will read it at once but using it you can make your code more concise:
array = CSV.read(, headers: true).map do |csv_row|
csv_row['Total']
end
Hope this helped.
First, you need to assign the returned value from $stdin.gets.chomp to a variable:
foo = $stdin.gets.chomp
Which will assign the entered input to foo.
You don't need to use $stdin though, as gets will use the standard input channel by default:
foo = gets.chomp
At that point use the variable as your read parameter:
zip_array = CSV.read(foo)
That's all basic coding and covered in any intro book for a language.
I need some help with a Ruby script I can call from the console. The script needs to parse a simple .txt file with comma separated values.
value 1, value2, value3, etc...
The values needs to be added to the database.
Any suggestions?
array = File.read("csv_file.txt").split(",").map(&:strip)
You will get the values in the array and use it to store to database. If you want more functions, you can make use of FasterCSV gem.
Ruby 1.9.2 has a very good CSV library which is useful for this stuff: http://www.ruby-doc.org/stdlib/libdoc/csv/rdoc/index.html
On earlier versions of Ruby you could use http://fastercsv.rubyforge.org/ (which essentially became CSV in 1.9.2)
You could do it manually by reading the file into a string and using .split(',') but I'd go with one of the libraries above.
Quick and dirty solution:
result = []
File.open("<path-to-file>","r") do |handle|
handle.each_line do |line|
result << line.split(",").strip
end
end # closes automatically when EOF reached
result.flatten!
result # => big array of values
Now you can iterate the result array and save the values to the database.
This simple file iteration doesn't take care for order or special fields, because it wasn't mentioned in the question.
Something easy to get you started:
IO.readlines("csv_file.txt", '').each do |line|
values = line.split(",").collect(&:strip)
# do something with the values?
end
Hope this helps.