MATLAB: Transferring all columns in all text files in a folder and graphing data

data with columnsdlmreadgraphing different colors each iterationgraphing with legendopen all files in folderreadtabletextscan

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

Best Answer

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.