create 'R'num2str​(m)'C'num2​str(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
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
Marco Yu 2016년 7월 11일
I have a image which is 200x200, let say it is A, so I know A(1,3)=2 I would want the variable R1C3=2 instead of A(1,3)=2 Because I need to do a linear modeling, I need to check which variable is important rather than the value itself. i.e. which location of the picture is important(contribute) more to the model.
P.S. I have 20 images so in the very very end the size of R1C3 will be 20x1. That's why I need a lots of variable to store the value.
e.g. R1C3(1)=2 , R1C3(2)=10, R1C3(3)=5
Stephen23
Stephen23 2016년 7월 11일
편집: Stephen23 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.
You really need to read about the xyproblem.

댓글을 달려면 로그인하십시오.

답변 (1개)

the cyclist
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
Marco Yu 2016년 7월 11일
This is not what I want sorry :( I need to create 200x200=40000 variables which named like I describe above. Each RmCn hold 1 value
Walter Roberson
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
Marco Yu 2016년 7월 11일
It is because I need to do a linear modeling analysis, therefore I can't do it in a matrix way but in a single variable way.
the cyclist
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
Marco Yu 2016년 7월 11일
I have a image which is 200x200, let say it is A, so I know A(1,3)=2 I would want the variable R1C3=2 instead of A(1,3)=2 Because I need to do a linear modeling, I need to check which variable is important rather than the value itself. i.e. which location of the picture is important(contribute) more to the model.
P.S. I have 20 images so in the very very end the size of R1C3 will be 20x1. That's why I need a lots of variable to store the value.
e.g. R1C3(1)=2 , R1C3(2)=10, R1C3(3)=5
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
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
Marco Yu 2016년 7월 11일
@walter Roberson Thank you, this is more likely an alternative method to tackle the problem.
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에 대해 자세히 알아보기

질문:

2016년 7월 11일

댓글:

2016년 7월 11일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by