Importing three .txt files into a structure and then do calculations and form graph.
2 ビュー (過去 30 日間)
古いコメントを表示
Hi
I have repeats of tests and would like to do some calculations using the data and then average across the samples.
Each is a .txt file has 33 colums with diff data outputs from the machine. The first 22 rows are of headers and important infomation from the machine then 100 data points[THE DATA]. However there is one last line of info that i do not need.
I would like to import the data of the files i.e.
- 1hr vs ungraf pdms -1N- low to high test 1.txt
- 1hr vs ungraf pdms -1N- low to high test 2.txt
- 1hr vs ungraf pdms -1N- low to high test 3.txt
so far this is what i have but for some reason it isnt outputting all the data.
clear
clf('reset')
[file, path] = uigetfile({'*.txt'},'Select the files you want to combine','MultiSelect','on');
file = cellstr(file);
N=length(file);
Names = strings(N,1);
for n = 1:3
%% Loads Data into Struct
Data(n).Name = char(file(n));
Data(n).Name = Data(n).Name(1:end-4); %gets rid of the '.txt' at the end of the filename
fileID = fopen(char(fullfile(path,file(n))));
New_Data(n) = textscan(fileID,'%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f','Headerlines',22,'CollectOutput',1);
fclose(fileID);
grid on
grid minor
end
3 件のコメント
Stephen23
2019 年 11 月 26 日
編集済み: Stephen23
2019 年 11 月 26 日
@Sufia Fatima: path is the name of an important inbuilt function. When you define a variable with this name, that function cannot be called. This is known as "shadowing" the function, and can cause unusual bugs and error which are difficult to debug. The easiest solution is to not use the names of any inbuilt functions (tip: use which to check if names are already used).
回答 (1 件)
Bhaskar R
2019 年 11 月 26 日
編集済み: Bhaskar R
2019 年 11 月 26 日
You said text file contains 31 columns but it has 19 columns only(attached file)
You are able to get data for 19 colums from your textscan as it is in text file.
I have made some modifications without file selection considering you are working in the prosent working directory
clear
fclose all;
% clf('reset')
% [file, path] = uigetfile({'*.txt'},'Select the files you want to combine','MultiSelect','on');
% file = cellstr(file);
% N=length(file);
% Names = strings(N,1);
% str is the variable common string for the three files
str = '1hr vs ungraf pdms -1N- low to high test ';
path = pwd; % assuming you are working in the present working directory
for n = 1:3
%% Loads Data into Struct
Name = [str, num2str(n), '.txt'];
Data(n).Name = [str, num2str(n)];
fileID = fopen(char(fullfile(pwd,Name)));
New_Data(n) = textscan(fileID,'%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f','Headerlines',22,'CollectOutput',1);
fclose(fileID);
end
Hope helps you
参考
カテゴリ
Help Center および File Exchange で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!