'num2str' Error

18 views (last 30 days)
Brodie Arthur
Brodie Arthur on 15 Aug 2019
Commented: Guillaume on 15 Aug 2019
Trying to have this result
Details- name:Tara Wilkins, ID:12344567, address:123 Happy Drv
Having errors when using num2str for the ID part, what am I doing wrong?
students(i) = struct();
students(1).ID = 12344567;
students(1).name = 'Tara Wilkins';
students(1).address = '123 Happy Drv';
students(2).ID = 15432123;
students(2).name = 'Fred Bloggs';
students(2).address = '125 Happy Drv';
students(3).ID = 34765765;
students(3).name = 'Jo Tamry';
students(3).address = '321 Happy Drv';
for i = 1:length(students)
disp(['Details- name:' ,students(i).name, 'ID:',students(i).[num2str(ID)], ', address:' ,students(i).address])
end
*Bolded the part that is the issue*

Accepted Answer

Guillaume
Guillaume on 15 Aug 2019
Yes, what you have written makes no sense at all. You want to convert the numeric content of students(i).ID to text, so that's what you should pass to num2str:
... num2str(students(i).ID)
However, instead of building your display string by concatenation and num2str, I would recommend you use sprintf. To me, it makes the whole thing easier to read:
disp(sprintf('Details- name: %s, ID: %d, address: %s', students(i).name, students(i).ID, students(i).address));
Note that I've added some spaces and commas between the different entries that your disp was missing. It's a lot easier with sprintf to see what the final result will look like.
You could also use fprintf directly instead of disp(sprintf(...)):
fprintf('Details- name: %s, ID: %d, address: %s\n', students(i).name, students(i).ID, students(i).address); %added a \n, line return for fprintf
  2 Comments
Brodie Arthur
Brodie Arthur on 15 Aug 2019
I just played around with it a bit more and got it working in Octave but not Matlab.
Havent been taught about fprintf yet only 3rd week in the topic.
this is my new result:
students(i) = struct();
students(1).ID = 12344567;
students(1).name = 'Tara Wilkins';
students(1).address = '123 Happy Drv';
students(2).ID = 15432123;
students(2).name = 'Fred Bloggs';
students(2).address = '125 Happy Drv';
students(3).ID = 34765765;
students(3).name = 'Jo Tamry';
students(3).address = '321 Happy Drv';
for i = 1:length(students)
disp(['Details- name:' ,students(i).name, [', ID:',num2str(students(i).ID)], ', address:' ,students(i).address])
end
Guillaume
Guillaume on 15 Aug 2019
The extra concatenation inside the outer concatenation is pointless
disp(['Details- name:' ,students(i).name, ', ID:', num2str(students(i).ID), ', address:', students(i).address])
would do the same.
Note that
students(i) = struct;
would only work if i is already defined (probably why it worked in octave, you already had an i variable). Even if it were replaced by students(3) = struct it wouldn't do what you want.
student = [];
or
clear student
would be better in this case.
Or you could create the structure in one go:
student = struct('ID', {12344567, 15432123, 34765795}, ...
'name', {'Tara Wilkins', 'Fred Bloggs', 'Jo Tamry'}, ...
'address', {'123 Happy Drv', '125 Happy Drv', '321 Happy Drv'});
Havent been taught about fprintf yet only 3rd week in the topic.
Nothing stops you from getting ahead of your course. You can read how fprintf works. It's a much better way to build text than string concatenation.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 15 Aug 2019
Since you're using release R2019a, rather than using char array concatenation or sprintf I would append strings together.
students(1).ID = 12344567;
students(1).name = 'Tara Wilkins';
students(1).address = '123 Happy Drv';
S = "Details - name: " + students(1).name + ...
" ID: " + students(1).ID + ...
" address: " + students(1).address
MATLAB will automatically convert the number 12344567 to the text data "12344567" when appending it to the rest of the string S. The ellipses aren't necessary, but I wanted to avoid a long line of code since it would add scroll bars to my message.
Though if your format isn't constrained (which it probably is, since this sounds like a homework assignment) I'd probably break name, ID, and address onto separate lines for readability.
S = "Details" + newline + "Name: " + students(1).name + newline + ...
"ID: " + students(1).ID + newline + ...
"address: " + students(1).address
and maybe indent the fields by one or two tab stops or a couple spaces.
tab = sprintf('\t');
S = "Details" + newline + ...
tab + "Name: " + students(1).name + newline + ...
tab + "ID: " + students(1).ID + newline + ...
tab + "address: " + students(1).address
spaces = blanks(7);
S2 = "Details" + newline + ...
spaces + "Name: " + students(1).name + newline + ...
spaces + "ID: " + students(1).ID + newline + ...
spaces + "address: " + students(1).address
  1 Comment
Guillaume
Guillaume on 15 Aug 2019
It might just be me, but I find it a lot easier to picture what the output will look like with
S = sprintf('Details- name: %s, ID: %d, address: %s\n', bunchofvariables) %or "" for string output
than with:
S = "Details - name: " + students(1).name + ...
" ID: " + students(1).ID + ...
" address: " + students(1).address
which is cluttered by variable names in the midst of the text.
The newline and tab add even more clutter compared to:
S = sprintf('Details\n\tname: %s\tID: %d\taddress: %s\n', bunchofvariables) %or "" for string output

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!