While loop does not repeat

1 次查看(过去 30 天)
To Tran Luan
To Tran Luan 2021-5-16
clc;close all;clear all;
[~,Spot]= xlsread('parkingspot.xlsx','A1:B4');
pick =choose();
spotcheck = strcmpi(Spot,'V');
spotcheck = 1;
while (spotcheck == 1)
spotcheck = {spotcheck};
pick =choose();
end
xlswrite('parkingspot.xlsx','V',1,pick);
The excel file that i load in:
Spot =
2×2 cell array
{'V'} {'V' }
{'V'} {0×0 char}
The pick =choose() function:
list = {'A1','A2','A3','A4','B1','B2','B3','B4'};
[pick,tf] = listdlg('PromptString',{'Select a Parking Spot.',...
'Checking if slot still available.',''},...
'SelectionMode','single','ListString',list);
switch pick
case 1
pick = 'A1';
case 2
pick = 'A2';
case 3
pick = 'A3';
case 4
pick = 'A4';
case 5
pick = 'B1';
case 6
pick = 'B2';
case 7
pick = 'B3';
case 8
pick = 'B4';
So the idea is if i pick "B4" it will check if B4 has a 'V' value in the cell, and if not it will repeat the choosing step, but the while() loop doesnt seem to work, please help

回答(1 个)

Cris LaPierre
Cris LaPierre 2021-5-16
编辑:Cris LaPierre 2021-5-16
Be careful. You are heading down the path of creating an infinite loop.
Your loop is not running multiple times right now due to the syntax error you are getting (the red error message).
spotcheck = 1;
% This line represents how you set spotcheck in your while loop
spotcheck = {spotcheck}
spotcheck = 1×1 cell array
{[1]}
% This line represents the comparison performed in your while loop conditional statement
spotcheck == 1
Operator '==' is not supported for operands of type 'cell'.
Perhaps you have removed some of your code in an attempt to simplify your example? The way to fix the error in what you have shared is to not use the curly braces when assigning a value to spotcheck.
  2 个评论
To Tran Luan
To Tran Luan 2021-5-17
I did not remove the curly brace by any attempt, the code is exactly how I upload, but the error is still happened
Cris LaPierre
Cris LaPierre 2021-5-17
If you do not remove them, you will continue to have this error.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by