主要内容

搜索

goc3
goc3
上次活动时间: 大约 3 小时 前

Untapped Potential for Output-arguments Block
MATLAB has a very powerful feature in its arguments blocks. For example, the following code for a function (or method):
  • clearly outlines all the possible inputs
  • provides default values for each input
  • will produce auto-complete suggestions while typing in the Editor (and Command Window in newer versions)
  • checks each input against validation functions to enforce size, shape (e.g., column vs. row vector), type, and other options (e.g., being a member of a set)
function [out] = sample_fcn(in)
arguments(Input)
in.x (:, 1) = []
in.model_type (1, 1) string {mustBeMember(in.model_type, ...
["2-factor", "3-factor", "4-factor"])} = "2-factor"
in.number_of_terms (1, 1) {mustBeMember(in.number_of_terms, 1:5)} = 1
in.normalize_fit (1, 1) logical = false
end
% function logic ...
end
If you do not already use the arguments block for function (or method) inputs, I strongly suggest that you try it out.
The point of this post, though, is to suggest improvements for the output-arguments block, as it is not nearly as powerful as its input-arguments counterpart. I have included two function examples: the first can work in MATLAB while the second does not, as it includes suggestions for improvements. Commentary specific to each function is provided completely before the code. While this does necessitate navigating back and forth between functions and text, this provides for an easy comparison between the two functions which is my main goal.
Current Implementation
The input-arguments block for sample_fcn begins the function and has already been discussed. A simple output-arguments block is also included. I like to use a single output so that additional fields may be added at a later point. Using this approach simplifies future development, as the function signature, wherever it may be used, does not need to be changed. I can simply add another output field within the function and refer to that additional field wherever the function output is used.
Before beginning any logic, sample_fcn first assigns default values to four fields of out. This is a simple and concise way to ensure that the function will not error when returning early.
The function then performs two checks. The first is for an empty input (x) vector. If that is the case, nothing needs to be done, as the function simply returns early with the default output values that happen to apply to the inability to fit any data.
The second check is for edge cases for which input combinations do not work. In this case, the status is updated, but default values for all other output fields (which are already assigned) still apply, so no additional code is needed.
Then, the function performs the fit based on the specified model_type. Note that an otherwise case is not needed here, since the argument validation for model_type would not allow any other value.
At this point, the total_error is calculated and a check is then made to determine if it is valid. If not, the function again returns early with another specific status value.
Finally, the R^2 value is calculated and a fourth check is performed. If this one fails, another status value is assigned with an early return.
If the function has passed all the checks, then a set of assertions ensure that each of the output fields are valid. In this case, there are eight specific checks, two for each field.
If all of the assertions also pass, then the final (successful) status is assigned and the function returns normally.
function [out] = sample_fcn(in)
arguments(Input)
in.x (:, 1) = []
in.model_type (1, 1) string {mustBeMember(in.model_type, ...
["2-factor", "3-factor", "4-factor"])} = "2-factor"
in.number_of_terms (1, 1) {mustBeMember(in.number_of_terms, 1:5)} = 1
in.normalize_fit (1, 1) logical = false
end
arguments(Output)
out struct
end
%%
out.fit = [];
out.total_error = [];
out.R_squared = NaN;
out.status = "Fit not possible for supplied inputs.";
%%
if isempty(in.x)
return
end
%%
if ((in.model_type == "2-factor") && (in.number_of_terms == 5)) || ... % other possible logic
out.status = "Specified combination of model_type and number_of_terms is not supported.";
return
end
%%
switch in.model_type
case "2-factor"
out.fit = % code for 2-factor fit
case "3-factor"
out.fit = % code for 3-factor fit
case "4-factor"
out.fit = % code for 4-factor fit
end
%%
out.total_error = % calculation of error
if ~isfinite(out.total_error)
out.status = "The total_error could not be calculated.";
return
end
%%
out.R_squared = % calculation of R^2
if out.R_squared > 1
out.status = "The R^2 value is out of bounds.";
return
end
%%
assert(iscolumn(out.fit), "The fit vector is not a column vector.");
assert(size(out.fit) == size(in.x), "The fit vector is not the same size as the input x vector.");
assert(isscalar(out.total_error), "The total_error is not a scalar.");
assert(isfinite(out.total_error), "The total_error is not finite.");
assert(isscalar(out.R_squared), "The R^2 value is not a scalar.");
assert(isfinite(out.R_squared), "The R^2 value is not finite.");
assert(isscalar(out.status), "The status is not a scalar.");
assert(isstring(out.status), "The status is not a string.");
%%
out.status = "The fit was successful.";
end
Potential Implementation
The second function, sample_fcn_output_arguments, provides essentially the same functionality in about half the lines of code. It is also much clearer with respect to the output. As a reminder, this function structure does not currently work in MATLAB, but hopefully it will in the not-too-distant future.
This function uses the same input-arguments block, which is then followed by a comparable output-arguments block. The first unsupported feature here is the use of name-value pairs for outputs. I would much prefer to make these assignments here rather than immediately after the block as in the sample_fcn above, which necessitates four more lines of code.
The mustBeSameSize validation function that I use for fit does not exist, but I really think it should; I would use it a lot. In this case, it provides a very succinct way of ensuring that the function logic did not alter the size of the fit vector from what is expected.
The mustBeFinite validation function for out.total_error does not work here simply because of the limitation on name-value pairs; it does work for regular outputs.
Finally, the assignment of default values to output arguments is not supported.
The next three sections of sample_fcn_output_arguments match those of sample_fcn: check if x is empty, check input combinations, and perform fit logic. Following that, though, the functions diverge heavily, as you might expect. The two checks for total_error and R^2 are not necessary, as those are covered by the output-arguments block. While there is a slight difference, in that the specific status values I assigned in sample_fcn are not possible, I would much prefer to localize all these checks in the arguments block, as is already done for input arguments.
Furthermore, the entire section of eight assertions in sample_fcn is removed, as, again, that would be covered by the output-arguments block.
This function ends with the same status assignment. Again, this is not exactly the same as in sample_fcn, since any failed assertion would prevent that assignment. However, that would also halt execution, so it is a moot point.
function [out] = sample_fcn_output_arguments(in)
arguments(Input)
in.x (:, 1) = []
in.model_type (1, 1) string {mustBeMember(in.model_type, ...
["2-factor", "3-factor", "4-factor"])} = "2-factor"
in.number_of_terms (1, 1) {mustBeMember(in.number_of_terms, 1:5)} = 1
in.normalize_fit (1, 1) logical = false
end
arguments(Output)
out.fit (:, 1) {mustBeSameSize(out.fit, in.x)} = []
out.total_error (1, 1) {mustBeFinite(out.total_error)} = []
out.R_squared (1, 1) {mustBeLessThanOrEqual(out.R_squared, 1)} = NaN
out.status (1, 1) string = "Fit not possible for supplied inputs."
end
%%
if isempty(in.x)
return
end
%%
if ((in.model_type == "2-factor") && (in.number_of_terms == 5)) || ... % other possible logic
out.status = "Specified combination of model_type and number_of_terms is not supported.";
return
end
%%
switch in.model_type
case "2-factor"
out.fit = % code for 2-factor fit
case "3-factor"
out.fit = % code for 3-factor fit
case "4-factor"
out.fit = % code for 4-factor fit
end
%%
out.status = "The fit was successful.";
end
Final Thoughts
There is a significant amount of unrealized potential for the output-arguments block. Hopefully what I have provided is helpful for continued developments in this area.
What are your thoughts? How would you improve arguments blocks for outputs (or inputs)? If you do not already use them, I hope that you start to now.
The Graphics and App Building Blog just launched its first article on R2025a features, authored by Chris Portal, the director of engineering for the MATLAB graphics and app building teams.
Over the next few months, we'll publish a series of articles that showcase our updated graphics system, introduce new tools and features, and provide valuable references enriched by the perspectives of those involved in their development.
To stay updated, you can subscribe to the blog (look for the option in the upper left corner of the blog page). We also encourage you to join the conversation—your comments and questions under each article help shape the discussion and guide future content.
Fayoz
Fayoz
上次活动时间: 2025-6-9,9:21

I bought standart licence no I cant get it
The following lines were added to the subplot function in version 2025a (line 291):
if ancestorFigure.Units == "normalized"
waitfor(ancestorFigure,'FigureViewReady',true);
end
That code isn't in version 2024a.
Because of this, I'm experiencing issues that cause the code to stop running when using subplot in this way:
figure('Units','normalized','Position',[0 0 0.3 0.3])
subplot(1,2,1)
...
Has anyone else encountered this error?
Does anyone understand the need for those lines of code?
JH
JH
上次活动时间: 2025-6-10,18:32

As far as I can tell, there is still no official support for creating publication-ready tables from regression output, either as latex or natively. Although MATLAB isn't primarily statistical software, this still seems like an oversight, as almost any similar software has this capability built-in or as a package.
Paul
Paul
上次活动时间: 大约 3 小时 前

Should plotting functions, such as plot, semilogx, etc. internally apply squeeze to inputs?
For example, the ubiquitous bode from the Control System Toolbox always returns 3D outputs
w = logspace(-1,3,100);
[m,p] = bode(tf(1,[1 1]),w);
size(m)
ans = 1×3
1 1 100
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
and therefore plotting requires an explicit squeeze (or rehape, or colon)
% semilogx(w,squeeze(db(m)))
Similarly, I'm using page* functions more regularly and am now generating 3D results whereas my old code would generate 2D. For example
x = [1;1];
theta = reshape(0:.1:2*pi,1,1,[]);
Z = [cos(theta), sin(theta);-sin(theta),cos(theta)];
y = pagemtimes(Z,x);
Now, plotting requires squeezing the inputs
% plot(squeeze(theta),squeeze(y))
Would there be any drawbacks to having plot, et. al., automagically apply squeeze to its inputs?
The ability to plot multiple signals on a plot and then use the plot browser to interactively control which ones are displayed has been one of the most useful features of the plotting tools and many of my scripts embed the command to open it after results analysis and plotting. It's been removed in 2025A with the comment that the Property Inspector provides the alternative. It doesn't. Having to go back into the menu to select the plot edit features to get to the Property Inspector (which doesn't provide an efficient alternative to the plot browser) has made the workflow very inefficient. Please bring it back a.s.a.p. !!!!
Due to MATLAB being banned in some mainland Chinese universities in 2020, in recent years a Chinese company called "Suzhou Tongyuan SoftControl" has completely imitated MATLAB’s behavior. Below are some screenshots as evidence. What is your opinion on this issue?
After waiting for a long time, the MathWorks official Community has finally resumed some of its functionalitys! Congratulations! Next, I’d like to share some thoughts to help prevent such outages from happening again, as they have affected far too many people.
  1. Almost all resources rely solely on MathWorks servers. Once a failure (or a ransomware attack) occurs, everything is paralyzed, and there isn’t even a temporary backup server? For a big company like MathWorks to have no contingency plan at all is eye-opening. This tells us that we should have our own temporary emergency servers!
  2. The impact should be minimized. For example, many users need to connect to the official servers to download various support packages, such as the Deep Learning Toolbox Converter for ONNX Model Format.” Could these be backed up and mirrored to the “releases” section of a GitHub repository, so users in need can download them.
  3. A large proportion of users who have already installed MATLAB cannot access the online help documentation. Since R2023a, installing the help documentation locally has become optional. This only increases the burden on the servers? Moreover, the official website only hosts documentation for the past five years. That means after 2028, if I haven’t installed the local offline documentation, I won’t be able to access the online documentation for R2023a anymore?
Anything else you’d like to add? Feel free to leave a comment.
@William Rose, Your dedication to helping others and sharing your knowledge is a big win for the community. Thanks for taking the time to contribute so thoughtfully - your impact is definitely being noticed.👏
Keep it up!
The attached code is an animated solution of the three body problem. On 2024b it runs perfectly fine. When we tried it on 2025a, the animation constantly hitches, the CPU usage is almost double and the runtime is much slower. The curves also look less detailed and jagged in some places. When we run it without drawing anything, the performance seems comparable between versions, but still slightly slower. All of this behavior persists across different hardware. Anybody else having this kind of problem with the new release? I'm suspecting the graphics backend changes may be the culprit here...
clc
clear
close
syms t x1(t) y1(t) x2(t) y2(t) x3(t) y3(t)
G = 6.6743 * 10^-11;
%epsilon = 1e-4
m1 = 10^12;
m2 = 1*10^12;
m3 = 1*10^12;
r1 = [x1(t),y1(t)];
K1 = 1/2 * m1 * (diff(x1(t),t)^2 + diff(y1(t),t)^2);
r2 = [x2(t),y2(t)];
K2 = 1/2 * m2 * (diff(x2(t),t)^2 + diff(y2(t),t)^2);
r3 = [x3(t),y3(t)];
K3 = 1/2 * m3 * (diff(x3(t),t)^2 + diff(y3(t),t)^2);
L1x = diff(diff(K1,diff(x1(t),t)) , t);
L1y = diff(diff(K1,diff(y1(t),t)) , t);
L2x = diff(diff(K2,diff(x2(t),t)) , t);
L2y = diff(diff(K2,diff(y2(t),t)) , t);
L3x = diff(diff(K3,diff(x3(t),t)) , t);
L3y = diff(diff(K3,diff(y3(t),t)) , t);
r12 = r2 - r1;
r13 = r3 - r1;
r23 = r3 - r2;
dlugosc_r12 = sqrt(r12(1)^2 + r12(2)^2);
dlugosc_r13 = sqrt(r13(1)^2 + r13(2)^2);
dlugosc_r23 = sqrt(r23(1)^2 + r23(2)^2);
Q12 = G * m1 * m2 / dlugosc_r12^2 * (r2-r1)/dlugosc_r12;
Q13 = G * m1 * m3 / dlugosc_r13^2 * (r3-r1)/dlugosc_r13;
Q23 = G * m2 * m3 / dlugosc_r23^2 * (r3-r2)/dlugosc_r23;
Q21 = -Q12;
Q32 = -Q23;
Q31 = -Q13;
Q1 = Q12 + Q13;
Q2 = Q21 + Q23;
Q3 = Q31 + Q32;
eqn_1_x = L1x == Q1(1);
eqn_1_y = L1y == Q1(2);
eqn_2_x = L2x == Q2(1);
eqn_2_y = L2y == Q2(2);
eqn_3_x = L3x == Q3(1);
eqn_3_y = L3y == Q3(2);
syms X1 Y1 X2 Y2 X3 Y3
Q1_num = subs(Q1,[x1(t), y1(t), x2(t), y2(t), x3(t), y3(t)],[X1, Y1, X2, Y2, X3, Y3]);
Q2_num = subs(Q2,[x1(t), y1(t), x2(t), y2(t), x3(t), y3(t)],[X1, Y1, X2, Y2, X3, Y3]);
Q3_num = subs(Q3,[x1(t), y1(t), x2(t), y2(t), x3(t), y3(t)],[X1, Y1, X2, Y2, X3, Y3]);
syms vx1 vy1 vx2 vy2 vx3 vy3
state_dot = [
vx1;
vy1;
vx2;
vy2;
vx3;
vy3;
Q1_num(1)/m1;
Q1_num(2)/m1;
Q2_num(1)/m2;
Q2_num(2)/m2;
Q3_num(1)/m3;
Q3_num(2)/m3
];
f = matlabFunction(state_dot, 'Vars', {sym('t'), [X1; Y1; X2; Y2; X3; Y3; vx1; vy1; vx2; vy2; vx3; vy3]});
u0 = [-1e5; %x1
0; %y1
1e5; %x2
0; %y2
0; %x3
sqrt(3)*1e5; %y3
-11/2 * 1e-3; %vx1
11/2*sqrt(3)*1e-3; %vy1
-11/2 * 1e-3; %vx2
-11/2*sqrt(3)*1e-3; %vy2
11e-3; %vx3
0]; %vy3
tspan = [0, 1e9];
%options = odeset('RelTol', 1e-15, 'AbsTol', 1e-20);
[t_sol, u_sol] = ode45(f, tspan, u0);
t_anim = linspace(t_sol(1), t_sol(end), 5000);
u_anim = interp1(t_sol, u_sol, t_anim);
%%
% figure;
tor_1 = plot(u_anim(:,1), u_anim(:,2), 'r', 'LineWidth',1.5); hold on;
tor_2 = plot(u_anim(:,3), u_anim(:,4), 'g', 'LineWidth',1.5);
tor_3 = plot(u_anim(:,5), u_anim(:,6), 'b', 'LineWidth',1.5);
% xlabel('x [m]');
% ylabel('y [m]');
% legend('Ciało 1', 'Ciało 2', 'Ciało 3');
% title('Trajektorie ciał w układzie trzech ciał');
% axis equal
% grid on;
pozycja_1 = plot(u_anim(1,1),u_anim(1,2),'ro','markersize',10,'markerface','r'); hold on
pozycja_2 = plot(u_anim(1,3),u_anim(1,4),'go','markersize',10,'markerface','g');
pozycja_3 = plot(u_anim(1,5),u_anim(1,6),'bo','markersize',10,'markerface','b');
% xlim([-2e5,2e5])
% ylim([-2e5,2e5])
axis equal
for i = 1 : 1 : length(t_sol)
set(pozycja_1,'XData', u_anim(i,1),'YData', u_anim(i,2));
set(pozycja_2,'XData', u_anim(i,3),'YData', u_anim(i,4));
set(pozycja_3,'XData', u_anim(i,5),'YData', u_anim(i,6));
set(tor_1,'XData', u_anim(1:i,1),'YData', u_anim(1:i,2));
set(tor_2,'XData', u_anim(1:i,3),'YData', u_anim(1:i,4));
set(tor_3,'XData', u_anim(1:i,5),'YData', u_anim(1:i,6));
drawnow;
% pause(0.001);
end
During the past twelve months, PIVlab, a MATLAB Community Toolbox for particle interference velocimetry (a technique for fluid flow measurement) set a new record for all-time File Exchange downloads, surpassing one hundred thousand, dating back to 2010. It also recently eclipsed the 1000 downloads/month mark on File Exchange.
Congratulations to @William Thielicke and his team for this fantastic long term achievement and head over to the File Exchange to download and use it: PIVlab - particle image velocimetry (PIV) tool with GUI - File Exchange - MATLAB Central
I like this problem by James and have solved it in several ways. A solution by Natalie impressed me and introduced me to a new function conv2. However, it occured to me that the numerous test for the problem only cover cases of square matrices. My original solutions, and Natalie's, did niot work on rectangular matrices. I have now produced a solution which works on rectangular matrices. Thanks for this thought provoking problem James.
JH
JH
上次活动时间: 2025-5-14

Why is RoBERTa not available as a pretrained model? It is superior to BERT in many fields and has become more popular in the literature. For faster inference, you should offer DistilBERT, which is more modern than BERT but smaller/faster. The respository hasn't been updated in two years, which is a lifetime in the field of deep learning.
https://github.com/matlab-deep-learning/transformer-models
Large Languge model with MATLAB, a free add-on that lets you access LLMs from OpenAI, Azure, amd Ollama (to use local models) on MATLAB, has been updated to support OpenAI GPT-4.1, GPT-4.1 mini, and GPT-4.1 nano.
According to OpenAI, "These models outperform GPT‑4o and GPT‑4o mini across the board, with major gains in coding and instruction following. They also have larger context windows—supporting up to 1 million tokens of context—and are able to better use that context with improved long-context comprehension."
You can follow this tutorial to create your own chatbot with LLMs with MATLAB.
What would you build with the latest update?
Hi,
We are looking for users of Simulink who also work with the Vehicle Network toolbox to attend a usability session. This wil be a 2 hour session and will offer $100 compensation.
If you are interested, please answer the questions below and send them to: usabilityrecruiting@mathworks.com
In the past 2 years, how often have you worked with ARXML (AUTOSAR XML) files in vehicle network communication?
a. At least 3-5 days per week
b. Once or twice a week
c. A few times a month
d. Once a month or less
e. Never
-
3. Have you worked with automotive ethernet in the past?
a. Yes
b. No
-
4. Which of the following best describe your experience with Simulink? (select all that apply)
Study Screener Q4
a. I have used CAN/ CAN FD blocks (https://www.mathworks.com/help/vnt/can-simulink-communication.html)
b. I have used Simulink Buses
c. I have used Simulink Data Dictionaries
d. Other
-
Thank you!
Elaine
If you are interested in this session, just send an email with the answers to the following questions to usabilityrecruiting@mathworks.com
1. Which of the following best describes your experience with Design of Experiment (DOE)?
a. I regularly use DOE in my work and am comfortable designing experiments and analyzing results
b. I have used DOE in a few projects and understand its principles and applications
c. I have a basic understanding of DOE concepts but have limited practical experience
d. I have never used DOE but I’m interested in learning
-
2. Briefly describe one of your recent projects where you used/want to use DOE. What are the objectives and outcomes?
-
Thank you!
JH
JH
上次活动时间: 2025-5-15

This website is not very attractive or easy to navigate. It is difficult to even find this section - if you start at the Mathworks website, there is no community tab:
You have to go to Help Center, which takes you to documentation, and then click on Community (redirecting you from https://www.mathworks.com/help to https://www.mathworks.com/matlabcentral)
Once you get there it's still a mess
If I have a question, it's not clear whether I should go to MATLAB Answers, Discussions, or Communities. It's not clear what the People page is for, or why it's split off from Community Advisors and Virtual Badges. "Cody" isn't very self-explanatory, and people will only stumble on it by accident, this seems like it should be integrated with contests. Don't get me started on the mess of a Blogs page. My browser knows that I speak English, so why am I being served Japanese language blogs?
I know that web design isn't the main priority of Mathworks, but the website has a very early-2010's look that increasingly feels dated. I hope there will be more consideration given to web UI/UX in the future.
I recently started learning MATLAB and Simulink, and I’m exploring the MathWorks website for study materials. Are there any free tutorials, examples, or beginner guides available directly from their official site? Also, are there special sections for students or personal users to access learning content without needing a full license?