Read csv file 2 and 3rd row and store them in different matrices

3 views (last 30 days)
Hello Guys and Girls,
i have a .csv file which i attach and i want to read the first row and put it in a matrix and the second row and put it in another matrix as they are different values from a measurement.
I have written this script until now
clear
close all
addpath(genpath('../../Functions'))
%Parameters for GRR
appraisersCount = 4; gapCount =118; measurementsPerSample = 15;
amountOfLayers = 3;
% Choose Folder for Gap Analysis
URL = uigetdir('C:\_Daten\Messungen\');
% tic
% Open URL
appraisers = dir(URL); appraisers(1:2) = [];
counter = 0;
% for a = 1:appraisersCount
% samplesURL = [URL '\' appraisers(a).name];
% samples = dir(samplesURL); samples(1:2) = [];
% measurementsURL = [URL '\' appraisers(s+2).name];
measurements = dir(URL); measurements(1:2) = [];
% folderspec = 'C:\_Daten\Messungen\WS\Teil 8_1\Teil 8';
% D = dir('C:\_Daten\Messungen\WS\Teil 8_1\Teil 8\*.csv');
for s = 1:44
% measurementsURL = [URL '\' appraisers(s+2).name];
% measurements = dir(URL); measurements(1:2) = [];
% %
% for m = 1:measurementsPerSample
% counter=counter+1;
% measurementURL = [measurementsURL '\' measurements(m).name];
file=fullfile( URL, measurements(s,1).name);
% table=readtable(file);
fid = fopen( file );
%
frequencies = fgets(fid);
frequencies = sscanf(frequencies,'%f');
table=readtable(file);
%
% dataColumns = length(frequencies)*2; %*2 cause data contain real + imag
% stringData = fscanf(fid,'%s');
% stringData = strrep(stringData,',-1,',',"-1",');
% stringData = strrep(stringData,',1,',',"1",');
% stringData = strrep(stringData,',0,',',"0",');
% stringData = strrep(stringData,'","',' ');
% stringData = strrep(stringData,',"',' ');
% stringData = strrep(stringData,'",',' ');
% stringData = strrep(stringData,'"',' ');
% stringData = strrep(stringData,',','.');
%
% data = sscanf(stringData, '%f');
% %
% data = reshape(data,dataColumns,[]);
% data = data';
% data_1(s,:)=data;
% end
%end
end
% dataReal=data_1(:,1:2:end);
% dataIma=data_1(:,2:2:end);
% figure (1)
%
%
%
% plot (frequencies,dataReal(1:10,:),'r',frequencies,dataReal(11:20,:),'b',frequencies,dataReal(21:35,:),'k',frequencies,dataReal(36:46,:),'c',frequencies,dataReal(47:118,:),'g')
% xlabel ('Frequency in [Mhz]');
% ylabel ('Real Part of the Voltage')
% grid on
% figure (2)
% plot (frequencies,dataIma(1:10,:),'r',frequencies,dataIma(11:20,:),'b',frequencies,dataIma(21:35,:),'k',frequencies,dataIma(36:46,:),'c',frequencies,dataIma(47:118,:),'g')
%
% xlabel ('Frequency in [Mhz]');
% ylabel ('Imaginary Part of the Voltage')
% grid on
% figure (3)
%
% abstand =95.5:0.1:97.5';
%
% surf (dataReal(:,:),'LineStyle','none')
% colormap jet
% figure (4)
% surf (dataIma(:,:),'LineStyle','none')
% colormap jet
% phi=phase(dataReal,dataIma);
% xlabel ('Real part of Voltage');
% ylabel ('Imaginary Part of Voltage')
% grid on
% figure (5)
% plot (frequencies,phi(1:10,:),'r',frequencies,phi(11:20,:),'b',frequencies,phi(21:35,:),'k',frequencies,phi(36:46,:),'c',frequencies,phi(47:118,:),'g')
% xlabel ('Frequency in [Mhz]');
% ylabel ('Phase')
% grid on
% figure (6)
% plot(dataReal,dataIma)
% xlabel ('Real part of Voltage');
% ylabel ('Imaginary Part of Voltage')
% grid on
% diffreal=diff(dataReal);
% figure (7)
% surf (diffreal)
% absolut=mag(dataReal,dataIma);
% figure(8)
% grid on
% plot(frequencies,absolut(1:10,:),'r',frequencies,absolut(11:20,:),'b',frequencies,absolut(21:30,:),'k',frequencies,absolut(31:46,:),'c',frequencies,absolut(47:118,:),'g')
% ylabel('Magnitude of Voltage');
% xlabel('Frequency');
% grid on
% % %figure (2)
% % % plot (frequencies(30),data_1(:,60))
% % figure (8)
% % plot (frequencies(40:50,1),dataReal(6:19,40:50))
% % xlabel ('Frequencies')
% % ylabel ('Real part of Voltage')
% % % figure (9)
% % % plot (frequencies(125:137,1),phi(:,125:137))
% % figure (10)
% % plot (frequencies(20:49,1),dataIma(10:15,20:49))
ignore the comments please as they do not matter for now. Thank you for your help.

Accepted Answer

Akira Agata
Akira Agata on 26 Apr 2022
How about the following?
% File path
url = 'https://jp.mathworks.com/matlabcentral/answers/uploaded_files/978985/2022-04-22%2010-40-28.csv';
% Read data
% (1) Looking at your data, decimal separator is ','. So you have to
% explicitly indicate that by using 'DecimalSeparator' option.
% (2) Since 2nd and 3rd line starts with tab, MATLAB treats 1st line as
% a hedder line. So you have to explicitly states 'NumHeaderLines' = 0.
A = readmatrix(url,...
'DecimalSeparator', ',',...
'NumHeaderLines', 0);
% Extract each row with eliminating NaN
data1 = rmmissing(A(1,:));
data2 = rmmissing(A(2,:));
data3 = rmmissing(A(3,:));
% Try to plot??
figure
plot(data1, data2)
hold on
plot(data1, data3)

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!