Displaying properties defined in MATLAB application

How can properties defined in an application be displayed ? The properties have numerical values. I have tried using spinners and EditText Fields but there is an error in the data type.

13 个评论

There are lots and lots of way to display numeric and text values from an app. Your question isn't specific enought to provide a specific answer.
Could you show us the relevant lines of code that you've tried already? Also include the property values that you're trying to display and any error messages you get.
Thank you Adam for the answer. Please find the detailed explaination of the issue below:
properties (Access = private)
B = 0;
end
for k = 1:app.itr
for h = 1:1:(length(mat_avg_fy))
error_dB(h,k) = -(2*app.C*app.D*cos(app.C*atan(app.B*app.sa_curve(h) + app.E*(atan(app.B*app.sa_curve(h)) - app.B*app.sa_curve(h))))*(app.sa_curve(h) - app.E*(app.sa_curve(h) - app.sa_curve(h)/(app.B^2*app.sa_curve(h)^2 + 1)))*(mat_avg_fy(h) - app.D*sin(app.C*atan(app.B*app.sa_curve(h) + app.E*(atan(app.B*app.sa_curve(h)) - app.B*app.sa_curve(h))))))/((app.B*app.sa_curve(h) + app.E*(atan(app.B*app.sa_curve(h)) - app.B*app.sa_curve(h)))^2 + 1);
sum_error_dB(1,k) = sum(error_dB(:,k));
end
alpha = app.lr;
B_new = app.B - ((1/length(mat_avg_fy))*sum_error_dB(1,k)*alpha);
end
app.BSpinner.Value = app.B;
As you can see here, I want to display the property B in a spinner. The issue is that when I assign the value of the property to the UISpinner, there is a error. The error asks the value of 'app.B' to be a double scalar. When I run the code in a MATLAB script, the value of B turns out to be a double scalar, but in the App Designer there is an error. I read many threads related to the error on MATLAB Answers but couldn't solve the issue.
Thank You.
Any time you reference as error in the forum, you should include the entire error message (all of it). That will tell us what's wrong.
Where is this loop? It looks like some important stuff is missing between the properties defs and the for-loop. The for-loop should be within a function, I believe, and that function should be within the methods sections.
Let's start with the error message.
Thank You once again Adam. I am fairly new to the community hence, I was a bit sceptical about uploading the complete code.
Here is the complete picture of the error that I encounter while running the app. Below please find the code. The properties are used in the function 'datasort(app)'.
properties (Access = private)
B = 0;
C = 0;
D = 0;
E = 0;
end
methods (Access = private)
function datasort(app)
for k = 1:app.itr
for h = 1:1:(length(mat_avg_fy))
error_dB(h,k) = -(2*app.C*app.D*cos(app.C*atan(app.B*app.sa_curve(h) + app.E*(atan(app.B*app.sa_curve(h)) - app.B*app.sa_curve(h))))*(app.sa_curve(h) - app.E*(app.sa_curve(h) - app.sa_curve(h)/(app.B^2*app.sa_curve(h)^2 + 1)))*(mat_avg_fy(h) - app.D*sin(app.C*atan(app.B*app.sa_curve(h) + app.E*(atan(app.B*app.sa_curve(h)) - app.B*app.sa_curve(h))))))/((app.B*app.sa_curve(h) + app.E*(atan(app.B*app.sa_curve(h)) - app.B*app.sa_curve(h)))^2 + 1);
sum_error_dB(1,k) = sum(error_dB(:,k));
end
alpha = app.lr;
B_new = app.B - ((1/length(mat_avg_fy))*sum_error_dB(1,k)*alpha);
end
end
The rest of the code runs without issues if I remove the command that assigns the spinner value as the property value. On execution of the code, the value of the property B results to 0.78792.
Thank You.
I suggest putting in a couple of lines that display size() and type() of app.B before attempting the assignment to app.BSpinner.Value in order to debug the problem.
Hi Walter,
I did add the lines that you suggested but inspite of the variable size being [1 1] and the class being double the error is persistent.
According to the error, App.B is either not double, not a scalar, or it's neither. Could you add this line and show us the output?
disp(app.B) % <--- add this line
app.BSpinner.Value = app.B;
I would suggest using dbstop if error
I am wondering if the error really is on that line, or if it is on the line before, the plot() call.
Thabk You Adam and Walter. I think I have found the error. The value of app.B also has an imaginary component and that is why the Application (i.e MATLAB App Designer) shows an error. Surprisingly, if I run the same script in MATLAB the value of the same variable B is 1x1 double. I have come across something like this for the first time and I hope you can guide me. Please find the Fig.1 below the error along with the value of app.B displayed and in Fig.2 the same value of same variable if run in MATLAB script.
Fig. 1
Fig.2
The warning and the error are generated at different lines in the code. I'm not convinced that they are releated based on the error and warning messages.
Furthermore, the imaginary number is a double and is a scalar.
x = 1.3139 + 0.0069i;
class(x)
ans =
'double'
isscalar(x)
ans =
logical
1
In that case, what would be a good approach to solve the issue ? Like I said, I have tried running the same logical statements in a MATLAB script and the result appear to be fine. Is there something specific (for eg:- use of 'for loops' ) that needs to be given special attention while creating a Application ?
Create two output fields. Set one to be the real part of B, and set the other one to be the imaginary part.
Thanks Walter. I created two output fields and the issue is resolved.

回答(1 个)

I just opened app designer and tried to set the value of a spinner to 1.3139 + 0.0069i. I received the same error as you.
'Value' must be a double scalar.
This is a poorly written error because 1.3139 + 0.0069i is a double and is a scalar.
class(1.3139 + 0.0069i)
ans =
'double'
isscalar(1.3139 + 0.0069i)
ans =
logical
1
But it makes sense that the spinner should be a real number.
The solution is to fix the problem with the value of app.B. My guess is that it should not be an imaginary number. Look into how app.B is being computed. If it should be an imaginary number, it's not clear to me how that would be represented in a spinner object.

1 个评论

Thank you, Adam. I have managed to fix the problem. The value of the variable is still has an imaginary part which should not have been the case. But while assigning the value to the spinner I have used the commal 'real(app.B)' and that has solved the problem. I do intend to analyse the reason as to why the variable is taking up an imaginary value but I feel it will need some time.

此问题已关闭。

标签

关闭:

2021-8-20

Community Treasure Hunt

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

Start Hunting!

Translated by