필터 지우기
필터 지우기

While loop does not repeat

조회 수: 1 (최근 30일)
To Tran Luan
To Tran Luan 2021년 5월 16일
댓글: Cris LaPierre 2021년 5월 17일
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