Using num2str with negative numbers

11 次查看(过去 30 天)
Hi, i'm working with num2str but I can't get a coherent matlab answer.
if the numeros matrix doesn't have a negative number it works like the following
clear all
clc
numeros=[1.5 3.4 5.3 7.2;
2.5 4.6 6.5 8.7];
parametro_numero_datos = ['N' num2str(length(numeros),'%d')]
j=0;
for i = 0 : 2 : length(numeros)+2
requestID(i+1,:) = ['i' num2str(numeros(1,j+1),'%-5.2f')];
requestID(i+2,:) = ['d' num2str(numeros(2,j+1),'%-5.2f')];
j=j+1;
end
requestID
the answer is:
parametro_numero_datos =
'N4'
requestID =
8×5 char array
'i1.50'
'd2.50'
'i3.40'
'd4.60'
'i5.30'
'd6.50'
'i7.20'
'd8.70'
but if i have a negative number in the numeros matrix, it doesn't work
clear all
clc
numeros=[1.5 -3.4 5.3 7.2;
2.5 4.6 6.5 8.7];
parametro_numero_datos = ['N' num2str(length(numeros),'%d')]
% requestID = ['i' num2str(numeros(1,1),'%.4f')]
% requestID = ['d' num2str(numeros(2,1),'%.4f')]
j=0;
for i = 0 : 2 : length(numeros)+2
requestID(i+1,:) = ['i' num2str(numeros(1,j+1),'%-5.2f')];
requestID(i+2,:) = ['d' num2str(numeros(2,j+1),'%-5.2f')];
j=j+1;
end
requestID
and the answer is:
parametro_numero_datos =
'N4'
Unable to perform assignment because the size of the left
side is 1-by-5 and the size of the right side is 1-by-6.
Error in Concatenacion_string_numero (line 15)
requestID(i+1,:) = ['i'
num2str(numeros(1,j+1),'%-5.2f')];
>>
colud you please help me?

回答(2 个)

Star Strider
Star Strider 2019-2-28
It is best to go with cell arrays here:
j=0;
for i = 0 : 2 : length(numeros)+2
requestID{i+1} = ['i' num2str(numeros(1,j+1),'%-5.2f')];
requestID{i+2} = ['d' num2str(numeros(2,j+1),'%-5.2f')];
j=j+1;
end
although using the sprintfc (undocumented) or the compose (link) function would be your best option, avoiding the loop entirely:
i = 0 : 2 : length(numeros)+2;
requestID(i+1) = sprintfc('i%-5.2f', numeros(1,:));
requestID(i+2) = sprintfc('d%-5.2f', numeros(2,:));
similarly:
i = 0 : 2 : length(numeros)+2;
requestID(i+1) = compose('i%-5.2f', numeros(1,:));
requestID(i+2) = compose('d%-5.2f', numeros(2,:));
Experiment to get the result you want.

Juan Jose Ortiz Torres
Ok, but I does not work. Could you please give me all the correct code?, because I tried but it failed again even both ways.
I run the following:
clear all
clc
numeros=[1.5 3.4 5.3 7.2;
2.5 4.6 6.5 8.7];
parametro_numero_datos = ['N' num2str(length(numeros),'%d')]
% requestID = ['i' num2str(numeros(1,1),'%.4f')]
% requestID = ['d' num2str(numeros(2,1),'%.4f')]
j=0;
for i = 0 : 2 : length(numeros)+2
requestID(i+1) = sprintfc('i%-5.2f', numeros(1,:));
requestID(i+2) = sprintfc('d%-5.2f', numeros(2,:));
j=j+1;
end
requestID
the answer was:
parametro_numero_datos =
'N4'
Unable to perform assignment because the indices on
the left side are not compatible with the size of the
right side.
Error in Concatenacion_string_numero (line 15)
requestID(i+1) = sprintfc('i%-5.2f',
numeros(1,:));
then, I tried with:
clear all
clc
numeros=[1.5 3.4 5.3 7.2;
2.5 4.6 6.5 8.7];
parametro_numero_datos = ['N' num2str(length(numeros),'%d')]
% requestID = ['i' num2str(numeros(1,1),'%.4f')]
% requestID = ['d' num2str(numeros(2,1),'%.4f')]
j=0;
for i = 0 : 2 : length(numeros)+2
requestID(i+1) = compose('i%-5.2f', numeros(1,:));
requestID(i+2) = compose('d%-5.2f', numeros(2,:));
j=j+1;
end
requestID
And it does not work
parametro_numero_datos =
'N4'
Unable to perform assignment because the indices on
the left side are not compatible with the size of the
right side.
Error in Concatenacion_string_numero (line 15)
requestID(i+1) = compose('i%-5.2f',
numeros(1,:));
>>
HELP please
  2 个评论
Juan Jose Ortiz Torres
I got the correct one:
clear all
clc
numeros=[1.5 -3.4 5.3 7.2;
2.5 4.6 6.5 8.7];
parametro_numero_datos = ['N' num2str(length(numeros),'%d')]
% requestID = ['i' num2str(numeros(1,1),'%.4f')]
% requestID = ['d' num2str(numeros(2,1),'%.4f')]
j=0;
for i = 0 : 2 : length(numeros)+2
requestID(i+1,:) = sprintfc('i%-5.2f', numeros(1,j+1));
requestID(i+2,:) = sprintfc('d%-5.2f', numeros(2,j+1));
j=j+1;
end
requestID
The output is
parametro_numero_datos =
'N4'
requestID =
8×1 cell array
{'i1.50 '}
{'d2.50 '}
{'i-3.40'}
{'d4.60 '}
{'i5.30 '}
{'d6.50 '}
{'i7.20 '}
{'d8.70 '}
Star Strider
Star Strider 2019-3-13
Don’t use a loop. Just do this:
numeros=[1.5 -3.4 5.3 7.2;
2.5 4.6 6.5 8.7];
i = 0 : 2 : length(numeros)+2;
requestID(i+1) = sprintfc('i%-5.2f', numeros(1,:));
requestID(i+2) = sprintfc('d%-5.2f', numeros(2,:));
Then:
Result = requestID
produces:
Result =
1×8 cell array
Columns 1 through 7
{'i1.50 '} {'d2.50 '} {'i-3.40'} {'d4.60 '} {'i5.30 '} {'d6.50 '} {'i7.20 '}
Column 8
{'d8.70 '}

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Variables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by