How do I change the name of a variable based on another variable, n?
Show older comments
sprintf('S0%d',n) = 2.*(I0final);
I have a function with input n, and I want to store the product of 2*(I0final) as S0n (ie for n = 0, I want 2*I0final to be stored as S00). However, I do not know how to change the name of the variable based on the input of n. Below is the rest of the code if needed.
function [S0,S1,S2,S3] = calcStokes(n,y)
full_name = fullfile('I0R', n);
full_named = fullfile('I0R', y);
I0value = I0(full_named,full_name);
I0ratio = calcI0ratio(I0value);
I0final = I0value/I0ratio;
full_name2 = fullfile('I1R', n);
I1value = I1(full_name2);
I1final = I1value./I0ratio;
full_name3 = fullfile('I2R', n);
I2value = I2(full_name3);
I2final = I2value./I0ratio;
full_name4 = fullfile('I3R', n);
I3value = I3(full_name4);
I3final = I3value./I0ratio;
sprintf('S0%d',n) = 2.*(I0final);
saveI(sprintf('S0%d',n))
sprintf('S1%d',n) = (2.*I1final)-(2.*I0final);
saveI(sprintf('S1%d',n))
sprintf('S2%d',n) = (2.*I2final)-(2.*I0final);
saveI(sprintf('S2%d',n))
sprintf('S3%d',n) = (2.*I3final)-(2.*I0final);
saveI(sprintf('S3%d',n))
Accepted Answer
More Answers (2)
Image Analyst
on 26 Jul 2015
You can't do this:
sprintf('S0%d',n) = 2.*(I0final);
because sprintf() produces a string. I think you just want to do
S0 = 2.*(I0final);
S1 = 2.*I1final - S0;
S2 = 2.*I2final - S0;
S3 = 2.*I3final - S0;
but I have no idea what your saveI() function is supposed to do.
1 Comment
Sulaiman Alvi
on 26 Jul 2015
Walter Roberson
on 26 Jul 2015
Edited: Walter Roberson
on 26 Jul 2015
And remember that you need to sprintf() what you pass into fullfile:
full_name = fullfile('I0R', sprintf('%d.bmp', n));
Categories
Find more on External Language Interfaces in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!