How can I join two parts of a char into just one?

8 visualizaciones (últimos 30 días)
Tay
Tay el 12 de Ag. de 2020
Comentada: Stephen23 el 24 de Nov. de 2021
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

Respuesta aceptada

Sudheer Bhimireddy
Sudheer Bhimireddy el 12 de Ag. de 2020
Editada: Sudheer Bhimireddy el 12 de Ag. de 2020
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'}
......

Más respuestas (2)

Steven Lord
Steven Lord el 12 de Ag. de 2020
I would use a string array in this case, not a char array.
numfiles = 9;
% Lenght parameters
step = 0.5;
start = 6;
final = 10;
str = "SiON_D4_L" + (start:step:final)
In fact, you can use singleton expansion to build an array of file names in one line.
str = "SiON_D4_L" + (start:step:final) + "_X" + (1:4).'

Dana
Dana el 12 de Ag. de 2020
I see a few things. First of all, it seems the reason for the error is that you're trying to assign something to the character array str that's of a different length than str(i,:). Character arrays work essentially like regular numeric arrays, where instead of each element of the array containining a single number, it contains a single character. This means that if you have a 5-character sequence, it needs to go into a string array that has 5 columns. This makes it a pain when you're trying to put strings of different lengths into the same array (which is what you're doing here). While you could make it work, there's a much easier way: use a cell array instead.
The second thing I notice is that, even if the above weren't a problem, your existing code would be overwriting the i-th row of str as you step through the values of j: you're assigning a new value to the same row of str for each value of j, which automatically replaces what was there before. I'm assuming you don't want that.
Third, I don't think the "%d" type is what you want in sprintf, since that's for integers. For example, with j=6.5, you'd get 'SiON_D4_L6.500000e+00', which is not what you want. In this application, you can actually just use the easier num2str function, which will automatically decide how to convert j to a string (and will give you the right answer here).
So here's how I might modify your code:
numfiles = 9;
% Lenght parameters
step = 0.5;
start = 6;
final = 10;
Lvc = (start:step:final); % store the array of L values you'll use
nL = numel(Lvc); % count the total number of L values
str = cell(numfiles,nL); % create a cell array, the (i,j)-th element of which
% corresponds to L(j)_xxxx_(i)umOf...
for i = 1:numfiles
stri = num2str(i); % convert i to its string representation
for j = 1:nL
strj = num2str(Lvc(j)); % conver the j-th element of Lvc to its string representation
% Here we assign the required string to the (i,j)-th element of the cell array str.
% Notice that, to access a single element of a cell array, you need to use braces {}
% rather than parentheses (). Also, for character arrays a, b, c, the syntax [a b c]
% concatenates them together.
str{i,j} = ['SiON_D4_L' strj '_12Lyrs_T_' stri 'umOfDistance.txt'];
end
end
%

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by