private variables in a matlab GUI

조회 수: 6 (최근 30일)
Robert
Robert 2023년 5월 17일
답변: Steven Lord 2023년 5월 17일
Hello,
I'm brand new to Matlab, but am knowledable enough to be dangerous as a coder in general. I'm just looking for a best practice here. I've created a GUI to run tests on a benchtop electronics assembly. In the GUI I would like to change settings, pass them to a function and store the results in an array of structures that contain all my test data.
This in the variable I would like to be my array of structures
properties (Access = private)
results;
end
This is what is executed when the user hits the "runTest" button.
% Button pushed function: RunTestButton
function run(app, event)
for x = 1:app.NumberofTestsEditField.Value
app.results(x) = runTest(app.paramX.Value,...
app.paramY.Value,...
app.paramZ.Value,...
)
pause(app.TimeBetweenTestsEditField.Value);
end
end
The error I get is it cannot covert a structure to a double. As stated, I am looking for best practices. runTest does indeed return a structure with some strings, doubles etc. I am presuming it thinks the results variable I defined as a private variable is of type double (I didn't tell it that). Do I just need need to init it as a variable that has the same structure that is being return for runTest()? I don't know how large it is going to be. Appreciate your patience as I am brand new at this :)
Thanks!
Bob

답변 (1개)

Steven Lord
Steven Lord 2023년 5월 17일
Before you enter the loop, by the way you've defined it the results property of the app is a double array. You can't assign a struct into an element of a double array; MATLAB doesn't know how to perform that conversion.
s = struct('x', 1, 'y', 2, 'z', 3);
x = 1:10;
x(2) = s % error
Conversion to double from struct is not possible.
You could initialize app.results to be a struct either in the definition of the property or as the first line of that method using the struct function.

카테고리

Help CenterFile Exchange에서 Write Unit Tests에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by