How to Loop on Different SNR values in MATLAB
2 views (last 30 days)
Show older comments
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")
0 Comments
Accepted Answer
Voss
on 12 Feb 2022
Edited: Voss
on 12 Feb 2022
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
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.
0 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!