Importing three .txt files into a structure and then do calculations and form graph.

2 ビュー (過去 30 日間)
Sufia Fatima
Sufia Fatima 2019 年 11 月 26 日
編集済み: Stephen23 2019 年 11 月 26 日
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 件のコメント
Sufia Fatima
Sufia Fatima 2019 年 11 月 26 日
Hi Sorry what do you mean?
Stephen23
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
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
  1 件のコメント
Sufia Fatima
Sufia Fatima 2019 年 11 月 26 日
Hi When i open it with excel it shows more of the colums. cos there are some that are empty
I got an error when i tried using the above code
>> mtmcodes
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in mtmcodes (line 28)
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);
If it is easeir i am only interested in columns 1 and 2 ( Rolling Speed (mm/s) , Traction Coeff (-)) and Ball Speed 1 and Disk Speed 1 in excel thats column 17 and 21

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by