MATLAB Answers

0

I have a problem for my semester work

Asked by Fabio Di Tomasso on 17 Jan 2013
Accepted Answer by Jan
I'm running r2010a.
I have a cell array of 3 columns and n rows in a data file named "telephones.mat' that has to be loaded.
The first column contains a last name, the second one a first name and the third one a telephone number. (all as strings)
I've got to create a program that, given a last name, it prints the first name and the telephone of the row. If there are more first names with the same last name, then the program will print all the people with the same last name, i mean their first names and telephone numbers.
The program will end when I choose to stop searching. (i should use a command like "continue?" with responses "Y" or "N")
i've done this:
:
load 'telephones.mat'
n=size(T,1);
next='Y';
cont =(next=='Y')|(next=='y');
for i=1:n
i=i+1;
E = input('Surname?','s')
if E == T(i,1)
fprintf(T(i,1:3))
end
next=input('continue? (y/n)','s');
cont=(next=='Y')|(next=='y');
end
I encounter problems with the eq signs... please help :/

  9 Comments

Jan
on 17 Jan 2013
No, Fabio, asking is not a crime, in opposite it shows your interest in solving the problem. It would only be cheating, if we post a working solution and you submit it without mentioning, that it has been (partially) written by somebody else - or if you try to hide your question by overwriting it after solutions have been posted. Fabio, I definitely dislike this, because it is against the nature of this forum and against my intention, when I spend time for answering. See Asnwers: Is deleting thread useful.
Any Matlab related questions are welcome in the forum. For homework questions posting full solutions would be counter productive. Deleting a question after answers have been posted is not respectful. Please ask the admins for restoring your question - you find the contact address on the bottom of this page.
ok, just did it because you scared me that my teacher would see it
Jan
on 18 Jan 2013
Do not let foreign forum users scare you. And if you are in doubt about a suiting reaction, asking others is the best strategy to avoid pitfalls.
You have reconstructed your message. Thanks! This will have a positive effect for your reputation in the forum (apart from the less meaningful reputation points).

Sign in to comment.

2 Answers

Answer by Jan
on 17 Jan 2013
Edited by Jan
on 17 Jan 2013
 Accepted Answer

It is not the nature of a FOR loop, that you increment the counter inside again:
for i=1:n
i=i+1; % *NO*
But the FOR loop is not needed at all here. Your variable "cont" seems, like you would rather prefer a while cont loop. Anyhow, a loop over all elements of T is not useful.
I suggest to stop an infinite loop, if the input name is empty:
while true
name = input('Name (empty to stop the loop):', 's');
if isempty(name)
break;
end
...
end
The error message means, that you cannot compare "E == T(i,1)", because the left hand side is a cell. strcmpi compares strings with cell strings even insensitive for the case. And you want compare with all names, which is "T(:, 1)". Then strcmpi replies all matching indices as logical vector.
You will get the next error soon, because fprintf cannot handle cells also. But as usual in Matlab, the documentation explains how to printf cell strings: doc fprintf.

  1 Comment

Jan, thank you very much I've never been taught the if isempty(name) or strcmpi... I had only found them when searching in google for any solutions, but could not use them... Anyway, thank you very much, I solved my problem!!! :)

Sign in to comment.


Answer by Jan
on 17 Jan 2013
Edited by Jan
on 17 Jan 2013

You forgot to ask a question. How could we help you without knowing where you currently stuck? Perhaps you want to decide for a Matlab version to install for solving this problem, or you cannot read the MAT file, or the output of the searched data are working and you don't know how to implement the stop method. Or everything works, but it is too slow.
Because this is a homework, posting a solution would be a drawback, because you cannot submit it without cheating anymore. But giving only vague hints is difficult, because the full program needs 7 lines only.

  1 Comment

sorry, i forgot to post my way of solving it... see it up... and it's not about cheating... it's the first semester that i am ever taught programming, and much worse Matlab, and professors in public universities here are not so willing to help me with my questions... could you then help me?

Sign in to comment.