Please help! Matrix Dimensions GUI Method Error

조회 수: 1 (최근 30일)
C_Man_03
C_Man_03 2021년 3월 28일
답변: Chidvi Modala 2021년 3월 31일
I am a high school senior and this is my first time creating a GUI in MATLAB. I'm trying to create a GUI where you can enter your age, weight, height, etc. and it will "assess" your choices. I'm running into errors when calling the methods. It keeps informing me that "Matrix Dimensions Must Agree" although I made sure all inputs are scalar doubles. I have left several other comments on lines of code that I am confused about, where although they do not seem to match with the variables at hand the app still compiles past those parts.
I have attatched a zip file containing my app as well as the four functions the app requires. Thank you!

채택된 답변

Chidvi Modala
Chidvi Modala 2021년 3월 31일
From a high level observation, few points can be noted
  • Casting a character array to double using "double()" will give you its ASCII values. For example the following code is taken from dailyAssessment.mlapp, line 163. Here user entered value is 100. then app.EnterWorkoutTime.Value will be '100' which is a character array. When you try to cast it to double, it gives you its ASCII values. So, app.workoutTime will be [49 48 48] (ASCII value of '1' is 49 and '0' is 48)
function EnterWorkoutTimeValueChanged(app, event)
value = app.EnterWorkoutTime.Value;
app.workoutTime = double(value);
app.workoutTime1 = double(value);
end
So you may replace double with str2double
function EnterWorkoutTimeValueChanged(app, event)
value = app.EnterWorkoutTime.Value;
app.workoutTime = str2double(value);
app.workoutTime1 = str2double(value);
end
  • In dailyHealth.m file, line 23, it is comparing a char array (workoutTime = [49 48 48]) with a scalar 30. Hence the error. So the above replacement can solve this error
if ((workoutTime >= 30) && (contains(workoutDesc,'Moderate'))) || ((workoutTime >= 15) && (contains(workoutDesc,'Vigorous')))
assessWorkout = workoutAssessmentAnswers(1);
else
assessWorkout = workoutAssessmentAnswers(2);
end
  • Based on the given code, the entered value will be stored only when the user changes the value from the given default value. For example, the following code is taken from dailyAssessment.mlapp, line 153
function ChooseSexSelectionChanged(app, event)
selectedButton = app.ChooseSex.SelectedObject;
if selectedButton == app.MaleButton
app.sex = 'M';
else
app.sex = 'W';
end
end
Initially the default value is set to 'M' If the user doesn't change it, then the above code doesn't execute, so app.sex will not be defined .
  • Just a suggestion, you can set a breakpoint wherever the error is occuring, and observe the workspace values to debug your issues. Here is a link that explains how to set a breakpoint.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by