MATLAB: variable names from array - arrays

I want to give a number to a variable, such that name of that variable is the ith component of array with variable names. Below is the
code I want to run, but I can't.
varname = {'test1','test2'};
rangenameraw = {'A','B','C'};
rangenamecolstart = {'1','11'};
rangenameend = {'10', '20'};
for i = 1:1:length(varname)
varname(i) =xlsread(filename, strcat(rangenameraw,rangenamecolstart,':'rangenameraw,rangenameend);
end

As far as I understand you want to create variables dynamically which can be achieved using the assignin function of MATLAB.
for i = 1:1:length(varname)
assignin('base', varname{i}, xlsread(filename, strcat(rangenameraw,rangenamecolstart,':'rangenameraw,rangenameend));
end

Related

Array Name being described as a String

I Have multiple arrays and I want to choose one to be my main array. I want the main Array to be described as a String. How can I name the new array by using my StationID String and my DirectionID String?
let Farbhof1 = ["hi","some","Strings"]
var StationID = "Farbhof"
var DirectionID = "1"
DestViewController.TableViewArray = "\(StationID)\(DirectionID)"
You cannot do that in Swift. Variable names need to be known at compile time, they cannot be calculated at runtime.

Need help getting perl array out of scalar context

I have a perl array I need to store in the following way:
$self->{spec}->{allImages} = #allImages;
Then I need to retrieve the contents later:
print Dumper($self->{spec}->{allImages});
This yields:
$VAR1 = 10;
(the number of items in the array).
How can I break out of scalar context and get $self->{spec}->{allImages} back as a list?
Each hash value can only be a scalar.
You must store a reference to the array:
$self->{spec}->{allImages} = \#allImages;
http://perldoc.perl.org/perlreftut.html will give you more tutorial.
You need to change the assignment:
$self->{spec}->{allImages} = \#allImages;
This creates an array-ref that you can use.

Defining Table or Array name as name from Previously Defined String in Matlab

So I would like to optimize my code such that I can look through an array such as
{'one','two','three'}
and create corresponding variables defined as tables or arrays
such as
one = table()
two = table()
three = table()
I am aware of the eval function however I would like to use this function in a loop s.t I allocate values to the new variable right after i create it
If I am understanding your question properly, given a cell array consisting only of strings, you wish to create variables in your workspace where each variable is declared as a string using the names from this cell array.
You could use eval, but I'm going to recommend something other than eval. eval should be avoided and instead of iterating those reasons, you can read Loren Shure's article on eval.
In any case, I would recommend you place these variables as dynamic fields inside a structure instead. Do something like this:
s = struct()
strs = {'one', 'two', 'three'};
for idx = 1 : numel(strs)
s.(strs{idx}) = table();
end
In this case, s would be a structure, and you can access the variable by the dot operator. In this case, you can access the corresponding variables by:
d = s.one; %// or
d2 = s.two; %// or
d3 = s.three;
If you want to place this into a function per se, you can place this into a function like so:
function [s] = createVariables(strs)
s = struct();
for idx = 1 : numel(strs)
s.(strs{idx}) = table();
end
This function will take in a cell array of strings, and outputs a structure that contains fields that correspond to the cell array of strings you put in. As such, you'd call it like so:
strs = {'one', 'two', 'three'};
s = createVariables(strs);
However, if you really really... I mean really... want to use eval, you can create your workspace variables like so:
strs = {'one', 'two', 'three'};
for idx = 1 : numel(strs)
eval([strs{idx} ' = table();']);
end
To place this into a function, do:
function [] = createVariables(strs)
for idx = 1 : numel(strs)
eval([strs{idx} ' = table();']);
end
However, be warned that if you run the function above, these variables will only be defined in the scope that the function was run in. You will not see these variables when the function exits. If you want to run a function so that the variables get defined in the workspace after you run the function, then eval is not the right solution for you. You should thus stick with the dynamic field approach that I talked about at the beginning of this post.
In any case, this will create one, two and three as workspace variables that are initialized to empty tables. However, I will argue with you that the first line of code is easier to read than the second piece of code, which is one of the main arguing points as to why eval should be avoided. If you stare at the second piece of code long enough, then you can certainly see what we're trying to achieve, but if you read the first piece of code, you can ascertain its purpose more clearly.

Store strings in a loop into an array?

I am working in MATLAB and currently have this code:
for i =1:142674:loop_end
data = textread('Results.txt', '%s');
name = data{i};
end
However, I want the name of the data point I select to be stored into an Array where the first name would be the first string in the array and so forth. So at the end I have an array containing all the names gathered from the loop.
What about this:
counter = 0
for i =1:142674:loop_end
counter = counter + 1;
data = textread('Results.txt', '%s');
myArray{counter} = data{i};
end
myArray will contain the names.
> myarray = 'Name1' 'Name2' 'Name3' 'Name4'
Though, it will actually be a Cell array, not a regular array
Why to read the text file multiple times ?
data = textread('Results.txt', '%s');
names = data(1:142674:end);
This way names is a cell array containing 1st, 142675th, etc... strings in the file.
NB: Well maybe I've misunderstood the question.

How to convert MATLAB output into an array?

I've made an M-file that outputs data into my MATLAB command window in the form below (I've deleted the extra spaces). Is there an easy way to convert this output into an array, without having to type all the data into the array editor as I'm currently doing? (Or even run it straight from the M-file into an array?)
T = 0.3000
price = 24.8020
T = 0.4000
price = 28.3453
T = 0.5000
price = 31.3934
T = 0.6000
price = 34.0880
Organize your data into arrays.
For example:
T=0.3:0.1:0.6;
Price=yourfunction(T);
Then if you want a price vs T graph,
plot(T,Price)
If you've got a large amount of data, try to avoid for loops, as they're slower than vectorized code.
At some point in your M-file you are printing each line of data to the command window, presumably using DISP or FPRINTF. You can replace that line with the following:
data = [data; T price];
Where T and price are the variables holding your data. Every time you call the above line (say, in a loop) it will append your data as a new row to the variable data. At some point at the beginning of your M-file, you would therefore have to add the following initialization:
data = []; %# An empty array
Appending values to an array like this can sometimes be inefficient, so if you already know ahead of time how many rows of data you will collect you can instead preallocate data with a given size. For example, if you know you will have 4 pairs of values for T and price, you can initialize data in the following way:
data = zeros(4,2); %# A 4-by-2 array of zeroes
Then, when you add data to the array you would instead do the following:
data(i,:) = [T price]; %# Fill row i with data
Another issue to consider is whether your M-file is a script or a function. A function M-file has a line like function output = file_name(input) at the top, whereas a script M-file does not. Running a script M-file is equivalent to typing the entire contents of the file directly into the MATLAB command window, so all of the variables created in the M-file are available in the workspace.
If you are using a function M-file, all variables created are local to the function, and any you want to use in the workspace will have to be passed as outputs from the function. For example, the top line of your function M-file could look like:
function data = your_file
where your_file is the name of the M-file and data is a variable being returned. When you call this function from the workspace you would then have to capture the output in a variable:
outputData = your_file();
Now you have the contents of the variable data from your_file stored as a new variable outputData in the workspace.
Why do you print out data instead of collecting it in an array?
M = [];
for ...
M(end+1, :) = [T, price];
end;
or, more idiomatically,
M = 0.3:0.1:0.6; % or whatever your T values should be
M = [M' (M'.^2)] % replace the .^2 by your price function

Resources