'Value' must be a double scalar.

I'm trying to make a function like intersect, union and ismember, but when I let it display the result, I get the error. "'Value' must be a double scalar.".
Here's the code:
function AfiarerezultatButtonPushed(app, event)
app.x = num2str(app.xEditField.Value);
app.y = num2str(app.yEditField.Value);
val = app.AlegereListBox.Value;
switch val
case 'intersect'
z = intersect(app.x, app.y);
app.zEditField.Value = double(z);
plot(app.UIAxes, z, "bo");
legend(app.UIAxes, 'x', 'y', 'z');
case 'union'
z = union(app.x, app.y);
app.zEditField.Value = double(z);
case 'ismember'
z = ismember(app.y, app.y);
app.zEditField.Value = double(z);
end

댓글 수: 3

Image Analyst
Image Analyst 2024년 1월 27일
Exactly which of those lines of code gives you that error? You forgot to attach the complete error. Please give us ALL THE RED TEXT including where it gives the line number and the actual line of code that threw the error and the complete function traceback.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
DINA
DINA 2024년 1월 27일
'Value' must be a double scalar.
Benjamin Thompson
Benjamin Thompson 2024년 1월 27일
Try setting a breakpoint in your function in MATLAB editor. Find the line causing the error and then fix the code. Looks like something needs to be converted to "double".

댓글을 달려면 로그인하십시오.

답변 (1개)

Hassaan
Hassaan 2024년 1월 27일
편집: Hassaan 2024년 1월 27일

0 개 추천

  1. Data Type Conversion: In your code, app.x and app.y are converted to strings with num2str. This is probably not what you want since intersect, union, and ismember functions expect numeric arrays or cell arrays of strings, not string representations of numbers.
  2. Plotting Issue: When you're trying to plot z in the intersect case, it might not always be a numeric vector that can be plotted directly, especially if app.x and app.y are strings.
  3. Assignment to app.zEditField.Value: You're converting z to a double and then trying to assign it to app.zEditField.Value. This will fail if z is not a single numeric value, but an array or empty.
  4. Logic Error in 'ismember': You're using ismember(app.y, app.y) which will always return an array of ones since every element in app.y is a member of app.y. You probably meant ismember(app.x, app.y).
function AfiarerezultatButtonPushed(app, event)
% Assuming app.xEditField.Value and app.yEditField.Value are numeric
x = app.xEditField.Value;
y = app.yEditField.Value;
val = app.AlegereListBox.Value;
switch val
case 'intersect'
z = intersect(x, y);
updateUIFieldAndPlot(app, z);
case 'union'
z = union(x, y);
updateUIField(app, z);
case 'ismember'
z = ismember(x, y);
updateUIField(app, z);
end
end
function updateUIField(app, z)
if length(z) == 1
app.zEditField.Value = z;
else
app.zEditField.Value = num2str(z);
end
end
function updateUIFieldAndPlot(app, z)
updateUIField(app, z);
if isnumeric(z) && ~isempty(z)
plot(app.UIAxes, z, "bo");
legend(app.UIAxes, 'x', 'y', 'z');
end
end
  • Proper Data Types: The function now uses the numeric values directly from the EditField components.
  • Flexible UI Updates: The updateUIField function sets the zEditField value depending on whether z is a single number or an array.
  • Conditional Plotting: The updateUIFieldAndPlot function handles plotting only if z is a numeric and non-empty array.
  • Corrected 'ismember' Logic: Now correctly checks if elements of x are in y.
Ensure that your EditField components are indeed providing numeric inputs as expected.
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

댓글 수: 2

DINA
DINA 2024년 1월 27일
편집: DINA 2024년 1월 27일
The error dissapeared but the app don't work. I press on the button and nothing happen.
DINA
DINA 2024년 1월 28일
'Value' must be a double scalar.
updateUIField(app, z);
updateUIFieldAndPlot(app, z);

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Live Scripts and Functions에 대해 자세히 알아보기

태그

질문:

2024년 1월 27일

댓글:

2024년 1월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by