How and when to use sprintf disp ?....

Hi guys, I m italian (excuse me for my english) and I have a question about sprintf (disp,..):
My code is :
for k=1:1:Matrice_Gruppi(1, end)
c1={nomi_famiglie};
c2={Matrice_Gruppi(5, k)};
sprintf('\n %s paga %s euro.',c1{1},c2{1});
end
I simply would
  1. Insert with keybord a string and to save it into nomi_famiglie then
  2. To print values of nomi_famiglie and Matrice_Gruppi on command window (inizially, then I would create a interface).
I see the error:
Undefined function or variable 'sprinf'.
Could you help me please?
Thanks and bye

4 件のコメント

Stephen23
Stephen23 2017 年 8 月 16 日
編集済み: Stephen23 2017 年 8 月 16 日
for k=1:1:Matrice_Gruppi(1, end)
c1={nomi_famiglie};
c2={Matrice_Gruppi(5, k)};
sprintf('\n %s paga %s euro.',c1{1},c2{1});
end
What is the point in making c1 and c2 cell arrays, when on the very next line you take the data out of the cells? The cell arrays are totally superfluous, and only serve to confuse.
Also, what does this do?:
for k=1:1:Matrice_Gruppi(1,end)
Does the first row of Matrice_Gruppi contain an enumeration?
Luca
Luca 2017 年 8 月 16 日
>> Matrice_Gruppi
Matrice_Gruppi =
1.0000
5.0000
4.0000
393.7000
520.2000
Stephen23
Stephen23 2017 年 8 月 16 日
@Luca Filippone: I think you should be using
for k = 1:numel(Matrice_Gruppi)
Luca
Luca 2017 年 8 月 16 日
編集済み: Luca 2017 年 8 月 16 日
All works, thanks

サインインしてコメントする。

 採用された回答

Image Analyst
Image Analyst 2017 年 8 月 15 日
編集済み: Image Analyst 2017 年 8 月 15 日

0 投票

There is no function sprinf(). Use sprintf(), with a "t", instead.
Or better yet, don't use cell arrays:
for k=1:1:Matrice_Gruppi(1, end)
myStrings{k} = sprintf('%s paga, %f euros.\n',...
nomi_famiglie, Matrice_Gruppi(5, k));
end
celldisp(myStrings);
assuming Matrice_Gruppi is an array of doubles, not strings like you implied by using %s.

5 件のコメント

Luca
Luca 2017 年 8 月 16 日
ok "sprintf"... :)
but I have..
Error using sprintf Function is not defined for 'cell' inputs.
Stephen23
Stephen23 2017 年 8 月 16 日
If Matrice_Gruppi is a cell array then use the correct indexing and format string:
for k=1:1:Matrice_Gruppi(1, end)
myStrings{k} = sprintf('%s paga, %s euros.\n', nomi_famiglie, Matrice_Gruppi{5,k});
end
You did not tell us what data class Matrice_Gruppi, not what it contains, so how are we supposed to tell you how to access its data correctly?
Stephen23
Stephen23 2017 年 8 月 16 日
Luca Filippone's "Answer" moved here:
..and then.. why with new=input('\n\nInserisci nome famiglia '); I can't type a string?
new=input('\n\nInserisci nome famiglia ')
Inserisci nome famiglia pippo Error using input Undefined function or variable 'pippo'.
Stephen23
Stephen23 2017 年 8 月 16 日
@Luca Filippone: because you forgot to read the input documentation, and learn that you need to use the 's' option for strings:
txt = 'Inserisci nome famiglia: ';
str = input(txt,'s')
Image Analyst
Image Analyst 2017 年 8 月 16 日
You showed in your comment above that Matrice_Gruppi is a 5 by 1 array. And Matrice_Gruppi(1,end) = 520.2000. So in your sprintf when you're trying to access Matrice_Gruppi(5, k) when k = 1,2,3,4,5,.....520, will fail because there is no columns beyond 1.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

質問済み:

2017 年 8 月 15 日

編集済み:

2017 年 8 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by