Looking up a string from a cell with user input
4 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I'm trying to work right now with a code, for which a userinputs a string, and the function matches that string to a rowxcolumn in a read xls file, and spits out the rowxcolumn information as a variable.
I tried using strcmp, but it seems to only work if the strings are in the same location. So for example
input = 'A,B,C,D,E,F'
list = 'D,C,A,B,F'
I would like it to output
ALocation = (3)
BLocation = (4)
CLocation = (2)
DLocation = (1)
ELocation = fprint( 'Error (E) not found')
Flocation = (5)
Any ideas on how I should go about this?
Thanks, ML
1 件のコメント
Jan
2013 年 6 月 23 日
Should "ALocation = (3)" be a string written to the command window or the assignment of a variable?
回答 (1 件)
Mark
2013 年 6 月 21 日
Hopefully this should help. I needed to do some random manipulation at the beginning, but I think newer releases of Matlab have functionality to make this much simpler...
close all
clear all
clc
input='A,B,C,D,E,F';
list='D,C,A,B,F';
in=input';
in(in==',')=[];
li=list';
li(li==',')=[];
[i,j]=ismember(in,li);
for k=1:size(in,1)
if ~isequal(j(k),0)
eval(strcat(in(k),'Location = ',num2str(j(k)),';'));
elseif isequal(j(k),0)
eval(strcat(in(k),'Location = ''Error (',in(k),') not found'';'));
end
end
Good luck!
3 件のコメント
Mark
2013 年 6 月 24 日
@Jan: The code provided was not meant for the OP to use as a simple copy and paste into their application, especially considering that the first few lines are very cumbersome... I understand those lines could be accomplished in a much more efficient way; however, with the older version I was running at the time, that was not possible for me to test.
Also, I have found that using a simple "else" can be dangerous. Often times, a variable can be assigned improperly or not as expected due to prior errors in the code. Considering this, I tend to explicitly check my conditions, reserving a pure "else" for error messages, warnings, or dbstop positions. Of course in this instance, doing so is not necessary and was only out of habit. Again, the methodology should have been conveyed to the OP so as to help them solve the issue in their own application.
While I am not a fan of the EVAL function myself, it exists for a reason and I have found myself forced to use it several times in the past. Because the OP was not entirely clear about the format of the output, I thought I would offer an example of the most difficult scenario (they clearly demonstrated a knowledge of the availability of fprintf and sprintf functions...) If you can describe a method in which a variable of unknown name may be created and assigned a value within a function, please describe that below as I would be very interested to learn of it...
Jan
2013 年 6 月 25 日
@Mark: There is no other method than EVAL to create variables dynamically (ignore the equivalent EVALIN and ASSIGNIN here). The trick is not to create variables dynamically at all. Instead of creating "ALocation", using Location.A is faster and clearer. Instering elements in a statically created array is much more efficient with respect to run- and debug-time, than creating a bunch of variables dynamically.
This is like storing real world objects: It is much smarter to put things into a cupboard instead of buidling a new box for every new piece.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!