how can I read and draw a plot and histogram from txt file on Matlab.

조회 수: 3 (최근 30일)
Yaren Duygu Atalay
Yaren Duygu Atalay 2024년 2월 21일
편집: Hassaan 2024년 2월 21일
Hallo, I have a problem with reading txt file.my data is in notepad it is a table basicly which seperate the data with ; sign. I want to read and draw a histogram with this data but there is an error by read or scan commands. My question is how can I read and draw a plot and histogram from this file on Matlab.
the table looks;
STATIONS_ID;MESS_DATUM; QN;FF_10;DD_10;eor
11;202307260800; 3; -999;-999;eor
11;202307260810; 3; -999;-999;eor
11;202307260820; 3; -999;-999;eor
11;202307260830; 3; -999;-999;eor
11;202307260840; 3; 2.1; 200;eor
11;202307260850; 3; 2.5; 200;eor
11;202307260900; 3; 2.8; 250;eor
11;202307260910; 3; 3.8; 260;eor
11;202307260920; 3; 4.5; 280;eor
11;202307260930; 3; 5.5; 240;eor
  댓글 수: 1
Aquatris
Aquatris 2024년 2월 21일
Would be benefical to add a sample txt file so someone can provide you the exact code. What kind of error are you getting? Did you try the built in 'import data' option which can also generate a script for you?
Example script generated by matlab for your example:
%% Setup the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 6);
% Specify range and delimiter
opts.DataLines = [2, Inf]; % 1 is the name of the column, VariableNames
opts.Delimiter = ";";
% Specify column names and types
opts.VariableNames = ["STATIONS_ID", "MESS_DATUM", "QN", "FF_10", "DD_10", "eor"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "categorical"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, "eor", "EmptyFieldRule", "auto");
% Import the data
tbl = readtable("C:\New Text Document.txt", opts);
%% Convert to output type
STATIONS_ID = tbl.STATIONS_ID;
MESS_DATUM = tbl.MESS_DATUM;
QN = tbl.QN;
FF_10 = tbl.FF_10;
DD_10 = tbl.DD_10;
eor = tbl.eor;

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

답변 (2개)

Chunru
Chunru 2024년 2월 21일
a = readtable("testdata.txt");
a.MESS_DATUM = string(a.MESS_DATUM)
a = 10x6 table
STATIONS_ID MESS_DATUM QN FF_10 DD_10 eor ___________ ______________ __ _____ _____ _______ 11 "202307260800" 3 -999 -999 {'eor'} 11 "202307260810" 3 -999 -999 {'eor'} 11 "202307260820" 3 -999 -999 {'eor'} 11 "202307260830" 3 -999 -999 {'eor'} 11 "202307260840" 3 2.1 200 {'eor'} 11 "202307260850" 3 2.5 200 {'eor'} 11 "202307260900" 3 2.8 250 {'eor'} 11 "202307260910" 3 3.8 260 {'eor'} 11 "202307260920" 3 4.5 280 {'eor'} 11 "202307260930" 3 5.5 240 {'eor'}
histogram(a.DD_10, 5) % 5bins

Hassaan
Hassaan 2024년 2월 21일
편집: Hassaan 2024년 2월 21일
@Yaren Duygu Atalay An intial attempt to the problem:
% Define the path to your data file
filePath = 'Test.txt'; % Ensure this path is correctly set
% Import the data
opts = delimitedTextImportOptions('Delimiter',';', ...
'VariableNamesLine', 1, ...
'DataLines', 2);
data = readtable(filePath, opts);
% Convert FF_10 to numeric if it's not already, handling possible conversion issues
if iscell(data.FF_10)
data.FF_10 = str2double(data.FF_10);
elseif ischar(data.FF_10)
data.FF_10 = str2double(cellstr(data.FF_10));
end
% Filter out rows with -999 in FF_10
data = data(data.FF_10 ~= -999, :);
% Plot histogram
figure;
histogram(data.FF_10);
title('Histogram of FF_10 Values');
xlabel('FF_10');
ylabel('Frequency');
-----------------------------------------------------------------------------------------------------------------------------------------------------
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.

카테고리

Help CenterFile Exchange에서 Other Formats에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by