While loop does not repeat

2 views (last 30 days)
To Tran Luan
To Tran Luan on 16 May 2021
Commented: Cris LaPierre on 17 May 2021
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

Answers (1)

Cris LaPierre
Cris LaPierre on 16 May 2021
Edited: Cris LaPierre on 16 May 2021
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 Comments
To Tran Luan
To Tran Luan on 17 May 2021
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 on 17 May 2021
If you do not remove them, you will continue to have this error.

Sign in to comment.

Categories

Find more on Downloads 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!