create 'R'num2str(m)'C'num2str(n) variables
显示 更早的评论
Hi, I am confuse on creating variable with 2 str e.g. R1C1 R1C2 R1C3...R1Cn then to R2C1 R2C2...RmCn
I know eval() is evil, but I need to create a lot of variable to store the data as I need to export it to other software.
As a result, I need to create 201X201 variable for analysis
Also, if possible, how can I use the new variable i.e. R1C1 to hold a value?
I have try temp_name='R'num2str(m)'C'num2str(n) then for human logic I would want to use the 'temp_name' as the variable name, how can I do it? I think I can't just do temp_name=1. I would want R1C1=1 instead.
Anyone can give me a suggestion please?
Thank you in advance :)
3 个评论
Stephen23
2016-7-11
@Marco Yu: You should read about they XY problem:
Instead of telling us what you think you need to do, why not actually explain your situation, and there are lots of experts here who can offer very good solutions, that might be very different to what you imagine. You have not told us anything about how this "other software" is going to use these variables, but most likely they will get exported or passed during some call operation of some kind... in either case, it is quite likely that it is not required to create lots of variables.
Marco Yu
2016-7-11
@Marco Yu: you have now told us that "I need to export it to other software" and that "I need to do a linear modeling", but you haven't told us the most important information: how are you going to get these value to that external software ? The two most likely ways, exporting and calling, would both be much much simpler and more robust when you use one variable. Would you pass all of these variables to a MATLAB function ? Save them somehow ? How do you imagine calling a MATLAB function with 40401 variables ? This has nothing to do with eval, that is simply terrible programming!
You need to understand: the advice to avoid eval is not just because eval is so slow and buggy, but because there are almost always better ways of achieving something than using eval. We can help you to write code in a better way, but you need to tell us what you need to do with this data: the data form that the external software requires, and how you will call that software.
回答(1 个)
the cyclist
2016-7-11
0 个投票
Use a cell array, and instead of the variable named "R1C3", write to the cell array element RC{1,3}.
9 个评论
Marco Yu
2016-7-11
Walter Roberson
2016-7-11
Why? Why do you need that many variables? You mention above that you need to export to other software, but what other software is it that expects individual MATLAB variables?
Marco Yu
2016-7-11
the cyclist
2016-7-11
Generally speaking, you can treat RC{1,3} (for example) exactly as you would treat a variable named R1C3.
Are you familiar with cell arrays? Did you read the documentation I referenced? It is not at all clear why you think this solution will not work for you.
Marco Yu
2016-7-11
Walter Roberson
2016-7-11
What program are you doing linear modeling in that needs to know what you named the variables inside MATLAB?
If you arrange the values in a fixed order, then by knowing which relative position the value is in, you can figure out the indices. For example,
Av = A(:); %make a 40000 x 1 vector from the 200x200 matrix A
[minvalue, minpos] = min(Av); %suppose you needed to figure out which variable held the minimum
[Ar, Ac] = ind2sub(size(A), minpos);
and now A(Ar, Ac) is the place that was (one of) the minimum values
Walter Roberson
2016-7-11
One thing to keep in mind is that if you are creating a text file of some kind of problem description to pass to external software, then what you write into the text file does not need to be exactly the same as the variable names you use in MATLAB. For example,
for J = 1 : size(A,1)
for K = 1 : size(A,2)
fprintf(fid, 'R%DC%D = %g\n', J, K, A(J,K));
end
end
R1C3 would be in the text file produced, but it does not need to exist inside of MATLAB.
Marco Yu
2016-7-11
the cyclist
2016-7-11
I hope Walter's solution is helpful for you. We may want to create a separate answer for it.
But don't give up on cell arrays. Note that your objection to it is still not valid. You can store a vector in a cell array element, just like another variable. For example,
RC = cell(6,6); % Define the cell array
RC{1,3} = zeros(20,1); % Store a vector of zeros in location 1,3
RC{1,3}([1 2 3]) = [2 10 5]'; % Set some values of that vector
RC{1,3} % See the result
Notice the use of curly brackets in some places to access the contents of the cell array, rather than the cell itself.
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!