how can I open any file with a .TXT file, read and print the data correctly?

2 views (last 30 days)
function graphData = graphData()
%This fucntion will make graphs comparing relevant parameters from my
%output.TXT files
%open the output.txt file
fileID = fopen('*.TXT', 'r');
%initialize variables
startRow = 6;
%format the data
formatSpec = '%3s%21s%4s%6s%6s%6s%10s%4s%6s%7s%4s%6s%12s%6s%s%[^\n\r]';
%turn the data from the file into a table
% textscan(fileID, formatSpec);
% dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string', 'ReturnOnError', false);
textscan(fileID, '%[^\n\r]', startRow-1, 'WhiteSpace', '', 'ReturnOnError', false, 'EndOfLine', '\r\n');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string', 'ReturnOnError', false);
%remove white space around columns
dataArray{1} = strtrim(dataArray{1});
dataArray{2} = strtrim(dataArray{2});
dataArray{3} = strtrim(dataArray{3});
dataArray{4} = strtrim(dataArray{4});
dataArray{5} = strtrim(dataArray{5});
dataArray{6} = strtrim(dataArray{6});
dataArray{7} = strtrim(dataArray{7});
dataArray{8} = strtrim(dataArray{8});
dataArray{9} = strtrim(dataArray{9});
dataArray{10} = strtrim(dataArray{10});
dataArray{11} = strtrim(dataArray{11});
dataArray{12} = strtrim(dataArray{12});
dataArray{13} = strtrim(dataArray{13});
dataArray{14} = strtrim(dataArray{14});
dataArray{15} = strtrim(dataArray{15});
dataArray{16} = strtrim(dataArray{15});
%close the text file
fclose(fileID);
%create a variable to store all output values
output = [dataArray{1:end}];
%convert string output to character output and then to a numeric output
dataChar = convertStringsToChars(output);
dataNum = str2double(dataChar);
%% Clear temporary variables
clearvars filename startRow formatSpec fileID dataArray ans;
%%plot the three graphs at once
clf
%lwd=2;
fsz=30;
figure(1);
%Sum the duration from output files
sumDur = dataNum(:,7);
graphData = plot(dataNum(:,2), dataNum(:,9));
grid minor
xlabel('Time (seconds)', 'FontSize', fsz)
ylabel('Frequency (KHz)', 'FontSize', fsz)
figure(2);
plot(dataNum(:,2), dataNum(:,8))
grid minor
xlabel('Time (seconds)', 'FontSize', fsz)
ylabel('Amplitude (db)', 'FontSize', fsz)
figure(3);
plot(dataNum(:,2), 17383/sumDur)
grid minor
xlabel('Time (seconds)', 'FontSize', fsz)
ylabel('Events', 'FontSize', fsz)
end

Answers (1)

dpb
dpb on 7 Jun 2019
fileID = fopen('*.TXT', 'r');
is invalid syntax-- fopen() can't use wild cards; tha'ts a dir functionality--
d=dir('*.TXT');
for i=1:numel(d)
fid=fopen(d(i).name, 'r');
...
But, looks like a very difficult and inefficient way to read a text file -- instead define the format of the file and read the numeric data directly.
Showing a small sample data file would help immeasurably.
There probably are also higher-level functions like readtable or even importdata to ease the task greatly as compared to lower-level routines. If it is as appears may be a fixed-width text file, using the detectImportOptions function for a fixedWidth import object is probably a help in abstraction level.

Categories

Find more on Printing and Saving in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!