'Value' must be a double scalar.
15 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Benjamin Thompson
le 27 Jan 2024
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".
Réponses (1)
Hassaan
le 27 Jan 2024
Modifié(e) : Hassaan
le 27 Jan 2024
- 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.
- 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.
- 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.
- 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 commentaires
Voir également
Catégories
En savoir plus sur Operators and Elementary Operations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!