one can use decimal symbol and digit grouping symbol in matlab simulink graphics

 Respuesta aceptada

Hi @Octavian,

Your task is to modify the Y-axis labels of a MATLAB plot to display the rotational speed in a more readable format. Specifically, instead of showing a speed of 10000 rpm, you want it to be formatted as 10,000 rpm.

% Modify Y-axis ticks to display in the desired format
yticks([0 5000 10000 15000 20000]); % Set specific Y-ticks
yticklabels({'0', '5,000', '10,000', '15,000', '20,000'}); % Custom labels

The yticks function is used to specify the locations of the ticks on the Y-axis. In this case, set ticks at 0, 5000, 10000, 15000, and 20000. The yticklabels function allows you to customize the labels for these ticks, format the label for 10000 as '10,000' to enhance readability. For more information on yticklabels, please refer to

https://www.mathworks.com/help/matlab/ref/yticklabels.html?searchHighlight=yticklabels&s_tid=srchtitle_support_results_1_yticklabels

As an example, please see attached.

Hope this helps.

8 comentarios

Also,as an option, I provided another code below to have you choose which one you like better. Here is code snippet.

% Sample data
x = 0:0.1:10; % Time or any other independent variable
y = 10000 * sin(x); % Example data representing rotational speed   in rpm
% Create the plot
figure;
plot(x, y, 'LineWidth', 2);
xlabel('Time (s)');
ylabel('Rotational Speed (rpm)');
title('Rotational Speed vs Time');
% Set Y-axis ticks and labels
yticks(0:2000:12000); % Define Y-axis ticks
yticklabels({'0 rpm', '2,000 rpm', '4,000 rpm', '6,000 rpm', 
'8,000 rpm', '10,000 rpm', '12,000 rpm'}); % Custom labels
% Optionally, you can also format the axes grid
grid on;

Please see attached.

Please also let me know if you any further questions.

You can use the set function in MATLAB to modify the Y-axis properties after running your simulation. Below is an example code snippet that you can include in your MATLAB script after the simulation:

% Run your Simulink model
sim('your_model_name'); % Replace 'your_model_name' with the   actual name of your model
% Assuming you're using a Scope block
h = findobj('Type', 'Figure'); % Get handles to all figures   (scopes)
% Loop through all figure handles
for i = 1:length(h)
  ax = get(h(i), 'CurrentAxes'); % Get current axes
  if ~isempty(ax) % Check if axes exist
      % Set Y-axis ticks and labels
      yticks = get(ax, 'YTick'); % Get current Y-ticks
      new_labels = strcat(num2str(yticks', '%,d'), ' rpm'); %         Format labels
        set(ax, 'YTickLabel', new_labels); % Update Y-tick labels
    end
  end

So, in the above code snippet,

sim('your_model_name'): This line runs your Simulink model.

findobj('Type', 'Figure'): Retrieves all figure objects (including scopes).

Loop through figures: For each figure, it checks for axes and retrieves the current Y-ticks.

num2str(yticks', '%,d'): Converts Y-tick values to strings with commas for thousands.

set(ax, 'YTickLabel', new_labels): Updates the Y-axis labels to include "rpm" and formatted numbers.

For more information on set function, please refer to

<https://www.mathworks.com/help/matlab/ref/set.html?searchHighlight=set%252520&s_tid=srchtitle_support_results_1_set%252520%20https://www.mathworks.com/help/matlab/ref/set.html?searchHighlight=set%2520&s_tid=srchtitle_support_results_1_set%2520 set function >

After implementing these changes, run your simulation again to confirm that the Y-axis displays correctly formatted values. By following these steps and using the provided code snippet, you should be able to format your Y-axis as desired in MATLAB Simulink. If you encounter any issues or need further customization, feel free to ask!

Thank you Umar!
Seeing in your Matlab code the instructions regarding yticks and yticks label I remembered that in the Property Editor of a Figure at the Y axis these characteristics can be accessed, and modifying the yticks label with the decimal symbol worked!
I need these changes for writing an article, and the editor told me to also change the format value of sample time Ts that appears on Powergui for discrete simulation time from 1e-05 s to 1x10-05 s. Do you have any idea how I can change the format and that's just for viewing

Hi @Octavian,

You mentioned the following, please see my response to your comments below.

Seeing in your Matlab code the instructions regarding yticks and yticks label I remembered that in the Property Editor of a Figure at the Y axis these characteristics can be accessed, and modifying the yticks label with the decimal symbol worked!

Thank you for your insightful feedback regarding the yticks and yticks label in my Matlab code. I appreciate your observation about accessing these characteristics through the Property Editor of a Figure.

I need these changes for writing an article, and the editor told me to also change the format value of sample time Ts that appears on Powergui for discrete simulation time from 1e-05 s to 1x10-05 s. Do you have any idea how I can change the format and that's just for viewing

To address sample time Ts in Powergui, while there is no direct method to change the display format within the GUI itself, you can adjust the format in your MATLAB script. You can use the sprintf function to format the output as desired. For example:

Ts = 1e-05; % Original sample time
formattedTs = sprintf('%.1fx10^{%d}', Ts/10^(-5), -5);
disp(formattedTs);

This will display the sample time in the format you specified. Remember, these changes are for viewing purposes only and do not affect the underlying data.

Hope this helps.

Hi Umar,
It help!
A last question: How do you make the exponent (i.s. -5) smaller than the rest of number, like to be superscript

Hi @Octavian ,

In MATLAB, you can achieve superscript formatting using TeX or LaTeX markup within the text function. By default, MATLAB uses TeX markup for text rendering, which allows for basic formatting options such as superscripts and subscripts. To represent a number with a superscript (like x^(-5)), you can utilize the following syntax:

    x = 1; % Example x value
    y = 1; % Example y value
    text(x, y, 'x^{-5}', 'Interpreter', 'tex');

For more information on text function, please refer to

https://www.mathworks.com/help/matlab/ref/text.html

In this example:

  • x and y are the coordinates where the text will be displayed.
  • x^{-5} indicates that the number -5 should be displayed as a superscript of x.
  • The Interpreter property is set to tex, which enables TeX formatting.

If you wish to use LaTeX for more complex formatting, you can change the interpreter to `'latex'`. Here’s how you would do it:

text(x, y, 'x^{\text{-5}}', 'Interpreter', 'latex');

This format is particularly useful when you want to combine different styles or symbols that may not be supported by TeX. Here is a complete example that plots a simple graph and includes the text with a superscript:

% Sample Data
x = linspace(0, 10, 100);
y = exp(-0.5 * x);
% Create Plot
figure;
plot(x, y);
hold on;
% Add Superscript Text
text(5, 0.2, 'e^{-5}', 'Interpreter', 'tex', 'FontSize', 14);
% Axis Labels
xlabel('Time');
ylabel('Amplitude');
title('Exponential Decay');
grid on;
hold off;

Using TeX or LaTeX markup in MATLAB's text function allows for effective representation of mathematical expressions, including superscripts. This capability enhances the clarity of visual data presentations and is particularly useful in academic and professional settings. By adjusting properties such as FontSize,you can further customize how your text appears within your plots.

Hi Umar,
It work very well/
Thank you!
Can you give me your email address for for other possible questions?
my email: g2tavi@yahoo.com
Hi @Octavian,
Thank you for your positive feedback! I'm glad to hear that everything is working well for you. Regarding your request for my email address, I prefer to keep our communication through this platform to ensure privacy and security. However, feel free to reach out with any questions here, and I'll be more than happy to assist you!

Iniciar sesión para comentar.

Más respuestas (1)

Hey Octavian,
In order to format the tick labels for the plots for a MATLAB figure, you can use the "ytickformat" function with "fmt" input argument. Refer to the following documentation: https://www.mathworks.com/help/matlab/ref/ytickformat.html#bvayhxa-1-fmt
x = 1:5;
y = [1000 3000 5000 7000 10000];
plot(x,y,'-V')
ytickformat('%,4.4g')
As far as I understand, this functionality is not directly available in Simulink Scopes. However I would suggest you to save the data into MATLAB workspace and create the figure in MATLAB itself. Have a look at the following blog by @Guy Rouleau: https://blogs.mathworks.com/simulink/2010/06/22/how-to-customize-the-simulink-scope/
I hope this helps!

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Productos

Versión

R2016a

Preguntada:

el 12 de Oct. de 2024

Comentada:

el 13 de Oct. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by