How do I change the name of a variable based on another variable, n?

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

I found several more errors in your code, like in how you call fullfile() and using variable names like I0 as functions to read in data, etc. I think this is closer to what you want, but there still may be errors:
function S = calcStokes(n,y)
full_name = sprintf('I0R%d', n);
full_named = sprintf('I0R%d', y);
I0value = read_I_file(full_named,full_name);
I0ratio = calcI0ratio(I0value);
I0final = I0value/I0ratio;
for k = 1 : n
inputBaseFileName = sprintf('I%dR', k)
Ivalue = read_I_file(inputBaseFileName);
Ifinal = Ivalue./I0ratio;
% Store the array for this iteration in a cell.
if k == 1
% Store 2 * Ifinal
S{k} = 2 * Ifinal;
else
% Store 2 * IFinal - S{1},
% which is the same as (2.*Ifinal)-(2.*I0final)
S{k} = 2 * Ifinal - S{1};
end
% Save all the S cells in individual files.
% First get the base file name.
outputBaseFileName = sprintf('S%d%d', k, n)
% Now prepend the folder name.
fullFileName = fullfile(folder, outputBaseFileName);
% Finally call saveI() which is our custom function.
saveI(fullFileName, S{k});
end

More Answers (2)

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

This would work, but I want to save each file with a different name based on what "n" value I give it, as stated above. saveI() saves the data for an image as a .tiff image with a name the same as the input. What you have above would not give me any variability with the naming.

Sign in to comment.

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!