Hello Everyone,
I have some data as attached. Then I have the code to plot the graph. But I have to convert my file to excel first.
Anyway have that I can plot grpah wothout convert my file to excel?
clc
clear all
close all
spec=readlines('nema2.spe'); % read as text
whos spec
data=readmatrix('nema2.spe.xlsx'); %the data from point1.prn must export to excel first.
whos data
%then from excel, it can be plotted.
plot(data(:,1),data(:,2))

 Respuesta aceptada

Star Strider
Star Strider el 8 de Ag. de 2024
There is no need to convert it to Excel. It is only necessary to tell readmatrix (introduced in R2019a) that it is a text file, since that is not obvious from the file extension. Otherwise, readtable (inttroduced in R2013b) will work, with a minor modification to the readmatrix code.
Try this —
Uz = unzip('name2.zip')
Uz = 1x1 cell array
{'name2.spe'}
A1 = readmatrix(Uz{1}, 'FileType','text')
A1 = 512x2
0.5000 0.4389 1.0000 0.4886 1.5000 0.5076 2.0000 0.5121 2.5000 0.5063 3.0000 0.4991 3.5000 0.4867 4.0000 0.4771 4.5000 0.4651 5.0000 0.4516
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x = A1(:,1);
y = A1(:,2);
figure
plot(x, y)
grid
T1 = readtable(Uz{1}, 'FileType','text')
T1 = 512x2 table
Var1 Var2 ____ _______ 0.5 0.43895 1 0.48862 1.5 0.5076 2 0.51209 2.5 0.50632 3 0.49912 3.5 0.48669 4 0.47707 4.5 0.46511 5 0.4516 5.5 0.43467 6 0.42078 6.5 0.4021 7 0.3875 7.5 0.37563 8 0.3562
x = T1{:,1};
y = T1{:,2};
figure
plot(x, y)
grid
.

4 comentarios

mohd akmal masud
mohd akmal masud el 8 de Ag. de 2024
Editada: mohd akmal masud el 8 de Ag. de 2024
Thank you @Star Strider. Its Work.
Then how to:
1) add title axis? let say i want my x axis is Energy(keV), my y axis is counts.
2) If I want to add the another graph in one figure, how? Let saya as attached is my another graph.
example:
colordepth
colordepth el 8 de Ag. de 2024
For a quick introduction to plotting and to enhance your MATLAB skills, I highly recommend completing the MATLAB Onramp course. This 2-hour course will guide you through the basics of MATLAB and make it easier to tackle similar problems in the future.
To address your problem of adding plot title and axis labels to the figure, you can use the MATLAB functionsxlabel, ylabel and title. Here’s a quick example:
xlabel('Energy (keV)')
ylabel('Counts')
title('Energy vs Counts')
For adding another plot to an existing plot, you can use the hold” statement as shown here:
UnzippedPoint1 = unzip('point1.zip')
A2 = readmatrix(UnzippedPoint1{1}, 'FileType', 'text');
x2 = A2(:,1);
y2 = A2(:,2);
hold on
plot(x2, y2)
hold off
legend('Dataset 1', 'Dataset 2')
Here’s the final output utilizing the 2 code snippets above:
If you wish to have the data as 2 separate subplots in the same window, you may use the “subplot” function linked at the bottom of this answer.
Here are some documentation links to learn more about functions used:
Hope this helps!
mohd akmal masud
mohd akmal masud el 8 de Ag. de 2024
Star Strider
Star Strider el 8 de Ag. de 2024
@mohd akmal masud — As always, my pleasure!
To add axis labels to a 2D plot, use xlabel and ylabel, and to add a title, use title. (There are similar functions for 3D plots and groups of plots such as those produced by the tiledlayout function.)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Preguntada:

el 8 de Ag. de 2024

Comentada:

el 8 de Ag. de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by