How to Loop on Different SNR values in MATLAB

Hi, I Hope you are doing well,
I want to make a Loop which takes different SNR value from array or cell. and save the mat file for each SNR
How can i do that?
i have the following code
But the files are overwritting How can i avoid overwrite
for SNR=[-20,-10,0,10,20,30]
channel = helperModClassTestChannel(...
'SampleRate', fs, ...
'SNR', SNR, ...
'PathDelays', [0 1.8 3.4] / fs, ...
'AveragePathGains', [0 -2 -10], ...
'KFactor', 4, ...
'MaximumDopplerShift', 4, ...
'MaximumClockOffset', 5, ...
'CenterFrequency', 902e6)
for p=1:numFramesPerModType
% Remove transients from the beginning, trim to size, and normalize
frame = helperModClassFrameGenerator(rxSamples, spf, spf, transDelay, sps);
% Save data file
fileName = fullfile(dataDirectory,...
sprintf("%s%s%s%03d_SNR",fileNameRoot,modulationTypes(modType),p,SNR));
save(fileName,"frame","label")

 Accepted Answer

Looks like you have it essentially right; you just need to use the decimal specifier %d (or %03d or whatever makes sense) rather than the string specifier %s in the sprintf format for the index p (and maybe some underscores for readability):
% Made up variables.
dataDirectory = '';
fileNameRoot = 'result';
modulationTypes = ["QAM" "PSK"];
modType = 1;
numFramesPerModType = 2;
for SNR=[-20,-10,0,10,20,30]
for p=1:numFramesPerModType
fileName = fullfile(dataDirectory,...
sprintf("%s_%s_%d_%03d_SNR",fileNameRoot,modulationTypes(modType),p,SNR))
end
end
fileName = "result_QAM_1_-20_SNR"
fileName = "result_QAM_2_-20_SNR"
fileName = "result_QAM_1_-10_SNR"
fileName = "result_QAM_2_-10_SNR"
fileName = "result_QAM_1_000_SNR"
fileName = "result_QAM_2_000_SNR"
fileName = "result_QAM_1_010_SNR"
fileName = "result_QAM_2_010_SNR"
fileName = "result_QAM_1_020_SNR"
fileName = "result_QAM_2_020_SNR"
fileName = "result_QAM_1_030_SNR"
fileName = "result_QAM_2_030_SNR"
The way you had it, with %s for p, the file names each contain a non-alphanumeric character (characters whose ASCII code is 1, 2, etc.). These gave me an error when using save() on my version of MATLAB, but here they seemed ok. In any case, if you want to see the value of p in the file name, use some variant of %d instead of %s.

More Answers (0)

Products

Release

R2021b

Asked:

on 12 Feb 2022

Edited:

on 12 Feb 2022

Community Treasure Hunt

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

Start Hunting!