I have a problem for my semester work

2 views (last 30 days)
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
Fabio Di Tomasso
Fabio Di Tomasso on 17 Jan 2013
ok, just did it because you scared me that my teacher would see it
Jan
Jan on 18 Jan 2013
Edited: 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.

Accepted Answer

Jan
Jan on 17 Jan 2013
Edited: Jan on 17 Jan 2013
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
Fabio Di Tomasso
Fabio Di Tomasso on 17 Jan 2013
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.

More Answers (1)

Jan
Jan on 17 Jan 2013
Edited: 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
Fabio Di Tomasso
Fabio Di Tomasso on 17 Jan 2013
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.

Categories

Find more on Scope Variables and Generate Names in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!