I need help with sort out string from Excel File to Matlab

1 次查看(过去 30 天)
This is the Excel part that i imported to Matlab
UserData =
4×2 cell array
{'30E92115' } {'abc123@gmail.com''}
{'HR26DK83OO'} {'xyz123@gmail.com'' }
{'29E492114' } {'yeetyeet@gmail.com'' }
{'30E591255' } {'travisscott@gmail.com'' }
My Main Code
PlateNumber ='30E92115';
[~,UserData] = xlsread('userdata.xlsx','A:B');
Plate = UserData(:,1);
Email = UserData(:,2);
noPlate = numel(Plate);
for i = 1:noPlate
compare = strcmpi(Plate,PlateNumber);
if compare == 1
Email = destination;
end
end
disp(destination);
So the idea is if the strcmpi() is true, when the PlateNumber value is equal to the email follow by the '30E92115' string in the imported Excel file, the "destination" will receive the email come along with the '30E92115' , but somehow I got this error, PLEASE HELP
Unrecognized function or variable 'destination'.
Error in compare (line 13)
disp(destination);

回答(1 个)

Thomas Jensen
Thomas Jensen 2021-5-10
Hi Tran,
The script is not assigning any value to the variable "destination". That is why the line "Email = destination;" and the line "disp(destination);" can be executed.
As a good practice, always assign a value to the variables set inside a loop or if statement. For example, add a new line before " for i = 1:noPlate" with the content "destination = [];".
Best regards,
  4 个评论
To Tran Luan
To Tran Luan 2021-5-11
I tried as your recommendation but the value of destination is still give out as emty
although the value of comp is 1 ,so the value of the second cell suppose to be equal to destination (the red markered email)
Thomas Jensen
Thomas Jensen 2021-5-11
Hi Tran,
Thanks for sending the screenshots.
I did some updates on your script and I think now it is working as you described.
PlateNumber ='30E92115';
[~,UserData] = xlsread('userdata.xlsx','A:B');
Plate = UserData(:,1);
Email = UserData(:,2);
noPlate = numel(Plate);
destination = [];
for i = 1:noPlate
compare = strcmpi(Plate{i},PlateNumber);
if compare == 1
destination = Email{i};
end
end
if isempty(destination)
disp('Email not found.');
else
disp(destination);
end
Best regards,

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by