I wish to change the name of col_1 to col_2, col_3....col_N for each value of 'j'. Could anyone suggest on how to handle this.? The reason why I wish to do this way is that size of col_i changes for different j. Any valuable suggestions and corrections are highly appreciated.
for j=1:N
for i=1:dum+1
col_1(i,1)=x;
col_1(i,2)=y;
end
end
you can use eval like the following:
for j=1:N
for i=1:dum+1
eval(strcat(strcat('col_',num2str(j)),'(i,1)=x'));
eval(strcat(strcat('col_',num2str(j)),'(i,2)=y'));
end
end
#KGV dynamic naming convention is not suggested in MATLAB. You have the indices already, you can call them easily. Don't try to rename/ use dynamic naming convention. You may read the below link for further information.
https://in.mathworks.com/matlabcentral/answers/105936-how-to-make-dynamic-variable-names-a1-a2-an-with-for-loop-using-eval-num2str
Related
I’m quite noob in Modelica language and I’d appreciate any help about this simple issue.
I’d like to know if it’s possible to write a variable name as a function of other variable in order to shorten the general code.
Here there is an example about what I’d like to do.
Thanks in advance!
model example
Real variable1;
Real variable2;
Real variable3;
equation
for i in 1:3 loop
variable(i)= […]
end for;
end example;
You probably want to use arrays in some way. This is similar to the code above, but perhaps you only need the array and not variable1 … variable3.
Real variable1;
Real variable2;
Real variable3;
Real variables[3] = {variable1,variable2,variable3};
equation
for i in 1:3 loop
variables[i] = /* … */;
end for;
I have the following function that I wish to solve using fzero:
f = lambda* exp(lambda^2)* erfc(lambda) - frac {C (T_m - T_i)}/{L_f*sqrt(pi)}
Here, C, T_m, T_i, and L_f are all input by the user.
On trying to solve using fzero, MATLAB gives the following error.
Undefined function or variable 'X'.
(where X are the variables stated above)
This error is understandable. But is there a way around it? How do I solve this?
This is answered to the best of my understanding after reading your question as it's not really clear what you are exactly trying and what you want exactly.
Posting the exact lines of code helps a big deal in understanding(as clean as possible, remove clutter). If then the output that matlab gives is added it becomes a whole lot easier to make sure we answer your question properly and it allows us to try it out. Usually it's a good idea to give some example values for data that is to be entered by the user anyway.
First of to make it a function it either needs a handle.
Or if you have it saved it as a matlab file you generally do not want other inputs in your m file then the variable.
So,
function [out]=yourfun(in)
constants=your values; %you can set a input or inputdlg to get a value from the user
out= something something, your lambda thingy probably; %this is the equation/function you're solving for
end
Now since that is not all that convenient I suggest the following
%declare or get your constants here, above the function makes it easier
syms lambda
f = lambda* exp(lambda^2)* erfc(lambda) - frac {C (T_m - T_i)}/{L_f*sqrt(pi)};
hf=matlabFunction(f); %this way matlab automatically converts it to a function handle, alternatively put #(lambda) in front
fzero(hf,x0)
Also this matlab page might help you as well ;)
I've tried to search stackoverflow for a solution, but the cases I found differ slightly from what I'm trying to do, so I thought I'd ask.
I have a loop in MATLAB, which for every iteration, a large matrix is computed. I want to save each matrix as a separate MAT file, however each file needs to be named according to its position in loop. For example: matrix1, matrix2,...
The method I'm using to save my data (which seemed different from the few examples I found) is the following (where matrix is the generated matrix and matrix1 is the filename to be saved for the matrix corresponding to i = 1)
save matrix1 matrix;
I've seen something similar to
save ['matrix', i] matrix;
But I can't seem to remember the exact syntax.
Sorry if the question is very basic, a simple nod in the right direction for this type of saving would be greatly appreciated.
Use the functional form of save:
save(['matrix', int2str(i)], 'matrix');
Here's my nod:
eval(['save matrix' num2str(i) ' matrix;']);
Good luck! :)
I am not a very hardcore coder in MATLAB, i have learned every thing from youtube and books. This might be a very simple question but i do not know what search for answer.
In MATLAB i am trying to do something like this.
>>[a,b,c] = [1,2,3]
and i want output like this.
>> a = 1
b = 2
c = 3
So Bsically question is that : - User will define the matrix of variables([a,b,c]) in staring of code and during process of the code similar matrix will be displayed and as input a matrix will be asked([1,2,3]). I dont know how do this without writing a loop code in which i will take every single variable from variable matrix and save the value in that variable by eval function.
well above written code is wrong and i know, i can do this with "for" loop and "eval" function.
but problem is that no. of variables(a,b,c) will never be constant and i want know if there exist any in built function or method in MATLAB which will work better than a for loop.
As i told earlier i don't know what to search for such a problem and either this is a very common question.
Either way i will be happy if you can at least tell me what to search or redirect me to a related question.
Please do write if you want any more information or for any correction.
Thank you.
The deal function can do this for a fixed number of inputs:
[A,B,C]=deal(1,2,3)
If you don't know how many inputs you will get beforehand, you have to do some fooling around. This is what I've come up with:
V=[1,2,3,4,5,6,7]
if length(V)>1
for i=1:length(V)
S{i}=['A' num2str(i)];
G{i}=['V(' num2str(i) ')'];
end
T=[S{1} ','];
R=[G{1} ','];
for i=2:length(V)-1
T=[T S{i} ','];
R=[R G{i} ','];
end
T=[T S{length(V)}];
R=[R G{length(V)}];
eval(['[' T ']=deal(' R ')'])
else
A1=V
end
But then dealing with A1, ... , An when you don't know how many there are will be a pain!
This is somehow known as "tuple unpacking" (at least it's what I would search in python!). I could find this thread which explains that you could do this in Octave (I checked and it works in Matlab also). You have to transform the vector into a cell array before:
values = num2cell([1,2,3])
[a,b,c] = values{:}
How can I access to dynamic variables in Matlab? I search for similar question but I didn't find.
Example (simplified):
for i=1:1
aux3=(i-1)*50;
delay_64_264(1,i) = mean(delay_64_264_', num2str(aux3), ' (:,3)*100;
end
What I want to do is mean of column 3 from variable delay_64_264_0.
Anyone can help me?
Thank you very much
You can use eval().
But I recommend not doing this at all. Use a multidimensional array, rather than lots of variables with slightly different names.
To follow on from Oli's suggestions, see this piece of the MATLAB FAQ:
http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
which shows how to use structures and cell arrays as an alternative to eval.