Transferring all columns in all text files in a folder and graphing data
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Sarah Brianne
el 26 de Ag. de 2018
Comentada: Sarah Brianne
el 6 de Oct. de 2018
Hello all! I am trying to develop a matlab code for my lab that will take all the .txt files in a folder with 5 columns of data. I then need to be able to separate the data by the columns and be able to graph certain ones against each other. I have tried so many things at this point. I am currently using readtable for impDAT(ii), I have also tried dlmread, importdata and textscan to no avail. I have also tried many different ways of formatting, I am getting an error with them all that it cannot open file. I will attach my code and if anyone has any advice would mean a lot. Also once I do this I need to figure out a way that every time it does a new text file it is a different color with a legend based on text filename. Thanks so much, I could graph manually but with all medias and trials I would spend days. :)
delimiterIn = ' ';
headlinersIn=1;
D = 'C:\Users\Sarah\Desktop\Sarah_2018\di no tape trial 1';
%S = dir(fullfile(D,'*.txt'));
F = dir(fullfile(D,'*.txt'));
for ii = 1:length(F)
%fid = fopen(F(ii).name);
impDAT(ii) = readtable('F(ii)');
%c(ii)= struct2cell(impDAT(ii))
%float(impDAT(ii).data)
%cell2mat(struct2cell(impDAT(ii).data))
DAT1(ii)= abs(impDAT(ii).data(:,3));
DAT2(ii)= (impDAT.data(:,2));
DAT3(ii)= (impDAT.data(:,1));
DAT4(ii)= (impDAT.data(:,4));
figure(1);
subplot(2, 2, 1);
plot(DAT2(ii), DAT1(ii), '.', 'Color', 'red');
titstr=sprintf('Nyquist Plot');
title(titstr,'FontSize',13);
xlabel("Z'/ohm");
ylabel('-Z"/ohm');
grid on;
hold on
subplot(2, 2, 2);
loglog(DAT3(ii), DAT4(ii), '.', 'Color', 'blue');
titstr=sprintf('Frequency Vs. Impedance Plot');
title(titstr,'FontSize',13);
xlabel('Freq/Hz');
ylabel('Z/ohm');
grid on;
hold off
end
% N = length(F);
% % loop for each file
% for i = 1:N
% %thisfile = files(i).name ;
% impDAT(i) = importdata(fid, delimiterIn, headerlinesIn);
% DAT1(i)= abs(impDAT.data(:,3));
% DAT2(i)= (impDAT.data(:,2));
% DAT3(i)= (impDAT.data(:,1));
% DAT4(i)= (impDAT.data(:,4));
%
% figure(1);
% subplot(2, 2, 1);
% plot(DAT2(i), DAT1(i), '.', 'Color', 'red');
% titstr=sprintf('Nyquist Plot');
% title(titstr,'FontSize',13);
% xlabel("Z'/ohm");
% ylabel('-Z"/ohm');
% grid on;
% hold on
%
%
% subplot(2, 2, 2);
% loglog(DAT3(i), DAT4(i), '.', 'Color', 'blue');
% titstr=sprintf('Frequency Vs. Impedance Plot');
% title(titstr,'FontSize',13);
% xlabel('Freq/Hz');
% ylabel('Z/ohm');
% grid on;
% hold off
%
%
% end
2 comentarios
Stephen23
el 27 de Ag. de 2018
@Sarah Brianne: today I fixed your code formatting for you. In future you can do it yourself: select the code text, click the {} Code button, ensure that there is an empty trailing line.
I also got rid of the uselesss # symbols from your tags, and added space characters between the words: you just need to use commas to separate the tags.
Please upload a sample file by clicking the paperclip button. Without a sample file it is very difficult for us to help you.
Respuesta aceptada
Stephen23
el 27 de Ag. de 2018
Editada: Stephen23
el 27 de Ag. de 2018
The basic problem is that you are not providing the directory when you read the file data, so whatever function you use will only look in the current directory. When you call dir you correctly added the directory path D using fullfile, you just need to do the same for when you read the file data:
D = 'C:\Users\Sarah\Desktop\Sarah_2018\di no tape trial 1';
S = dir(fullfile(D,'*.txt'));
C = cell(1,numel(S));
for ii = 1:numel(S)
F = fullfile(D,S(ii).name); % <- you forgot to include the directory here!
C{ii} = whateverFunctionReadsYourFile(F);
end
As you did not provide any sample file I have no idea what function you need to use to read it.
20 comentarios
Stephen23
el 6 de Oct. de 2018
Editada: Stephen23
el 6 de Oct. de 2018
@Sarah Brianne: dlmread does not work on files which contain non-numeric data (like that header section): the entire file should be a matrix of delimited numeric data for it to work properly.
fmt = repmat('%f',1,5);
opt = {'HeaderLines',8, 'CollectOutput',true};
... inside the loop:
[fid,msg] = fopen(F,'rt');
assert(fid>=3,msg)
C = textscan(fid,fmt,opt{:});
mat = C{1};
...
Adjust the textscan options to get it to work with your data file.
Más respuestas (0)
Ver también
Categorías
Más información sobre Annotations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!