MATLAB: How to join two parts of a char into just one

charindexloop

I want to load the files with the default name of "SiON_D4_L9_12Lyrs_T_1umOfDistance.txt" but the name changes in two places:
– on L_SomeNumber like "SiON_D4_L6_12Lyrs_T_1umOfDistance.txt", "SiON_D4_L6.5_12Lyrs_T_1umOfDistance.txt" and so on until 10.
– and at the end of the file _SomeNumberumOfDitance.txt like "SiON_D4_L6_12Lyrs_T_1umOfDistance.txt", "SiON_D4_L6_12Lyrs_T_2umOfDistance.txt" and so on until 9.
As the first part changes in steps of 0.5, I did a first loop as follows:
numfiles = 9;
% Lenght parameters
step = 0.5;
start = 6;
final = 10;
for i = 1:numfiles
for j = start:step:final
str(i,:) = sprintf('SiON_D4_L%d',j);
end
end
%
but I'm getting the error:
Unable to perform assignment because the size of the left side is 1-by-10 and the size of the right side is 1-by-21.
Error in Transmission_over_distance_D4 (line 15)
str(i,:) = sprintf('SiON_D4_L%d',j);
I was thinking about creating the first part of the char and then using the "join" command to join the first part with the second part and then have the whole name. In the end, I would call the file using the load command.
I have a diff code that is working but I had to put 9 loops to make it work. Now I'm trying to deacrease the code lines.
Any suggestion? Any help I would appreciate

Best Answer

Try this:
fileID = 1;
for i = 1:9
for j = 6:0.5:10
fileName{fileID,1} = strcat('SiON_D4_L',num2str(j),'_12Lyrs_T_',num2str(i),'umOfDistance.txt');
fileID = fileID+1;
end
end
disp(fileName);
>> {'SiON_D4_L6_12Lyrs_T_1umOfDistance.txt' }
{'SiON_D4_L6.5_12Lyrs_T_1umOfDistance.txt'}
{'SiON_D4_L7_12Lyrs_T_1umOfDistance.txt' }
{'SiON_D4_L7.5_12Lyrs_T_1umOfDistance.txt'}
{'SiON_D4_L8_12Lyrs_T_1umOfDistance.txt' }
{'SiON_D4_L8.5_12Lyrs_T_1umOfDistance.txt'}
......