Extract Values from String with sscanf

Hallo Guys and Gals,
i have following code:
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 3.5.2022\');
% 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');
rowscounter=41;
for s = 1:rowscounter
% 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 );
%
wellenlaenge = fgetl(fid);
wellenlaenge = sscanf(wellenlaenge,'%s');
%wellenlaenge = strrep(wellenlaenge,' ',';');
wellenlaenge = strrep(wellenlaenge,',','.');
wellenlaenge = sscanf(wellenlaenge,'%f');
%
% 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';
reflect(s,:)=data(:,1:125);
% end
%end
end
And i am trying to read the first line of the .csv file i posted with fgetl, nevertheless for some reason it wants to start the values of the first line with 0. while the values are like 902.1, rund the code as it is and you understand what i mean.

10 comentarios

Stephen23
Stephen23 el 5 de Mayo de 2022
Editada: Stephen23 el 5 de Mayo de 2022
Your code with the commented-out lines removed and some comments added:
URL = uigetdir('C:\_Daten\Messungen 3.5.2022\');
appraisers = dir(URL);
appraisers(1:2) = []; % buggy attempt to remove the dot directory names
counter = 0;
measurements = dir(URL);
measurements(1:2) = []; % another buggy attempt to remove the dot directory names
rowscounter=41;
for s = 1:rowscounter
file=fullfile( URL, measurements(s,1).name);
fid = fopen( file ); % file opened but never closed
wellenlaenge = fgetl(fid); % reads one line
wellenlaenge = sscanf(wellenlaenge,'%s');
wellenlaenge = strrep(wellenlaenge,',','.');
wellenlaenge = sscanf(wellenlaenge,'%f');
stringData = fscanf(fid,'%s'); % reads... whatever
stringData = strrep(stringData,',','.');
data = sscanf(stringData, '%f');
data = data';
reflect(s,:)=data(:,1:125);
end
What is the expected result of repeatedly opening the file inside the loop (without closing it)? Why not open it once before the loop? (of course closing it after the loop).
What does the variable name rowscounter mean? It does not seem to be related to anything about the file rows.
For robust removal of dot directory names use SETDIFF or ISMEMBER, or in your case by simply defining the DIR search string to include the filename (with wildcards as required) and file extension.
Your file looks like this:
What is the expected output from this file?
Alex Perrakis
Alex Perrakis el 5 de Mayo de 2022
Hello Stephen,
rowscount has nothing to do with this file you are right. The first row of the file i uploaded are wavelengths which are paired with signals the signals vary with each measurement so that's why i created a loop to save the signals. So the value of row counter is to pair with how many measurements i did.
And you are right to get the first line (meaning wavelengths) one must not open the .csv file inside the loop
Stephen23
Stephen23 el 5 de Mayo de 2022
@Alex Perrakis: What is the expected output from this file?
Alex Perrakis
Alex Perrakis el 5 de Mayo de 2022
I just want to import the first row of values into an array with scans. Like they appear. Did you run the script?
Stephen23
Stephen23 el 5 de Mayo de 2022
Editada: Stephen23 el 5 de Mayo de 2022
"I just want to import the first row of values into an array with scans. Like they appear."
Your code reads the first line using FGETL into a variable named wellenlaenge, the data of which you ultimately ignore and discard. The values you store in the (presumably) output array reflect are clearly from the second line of the file, read via FSCANF. So far everything you have described is consistent with this.
You wrote the code to discard the data from the first line, so that is exactly what it does. If you want to keep the data from the first line, then you will need to do something with the data in wellenlaenge (e.g. assign it to an array).
Note that you should preallocate reflect.
"Did you run the script?"
Out of curiousity, yes. But nothing unexpected happened.
Alex Perrakis
Alex Perrakis el 5 de Mayo de 2022
Editada: Alex Perrakis el 5 de Mayo de 2022
Well in reflect I wanted to store the second line of .csv file which works. The first line is the emission wavelength and the second is the signal that arrives at the sensor and I wanted to save them in two different arrays. Because I carried out many measurements the directory there are many .csv which I run in a loop and store their second line in reflect.Thanks for your interest bro and the solution. I will run it when I return to and let you know. Cheers from Germany.
Alex Perrakis
Alex Perrakis el 5 de Mayo de 2022
@Stephen why did you erase the answer man?
Alex Perrakis
Alex Perrakis el 10 de Mayo de 2022
@Stephen23 Hey stephen, why did you erase you answer man? i need it ;(
@Alex Perrakis: Remember. that you find deleted message in Google's cache. Simply search for a block of text, e.g.
"Well in reflect I wanted to store the second line of .csv file which works. The"
and open the cached version of the page. Then you will find Stephen's answer:
[fid,msg] = fopen('2022-05-03 11-16-00.csv','rt');
assert(fid>=3,'%s',msg)
str = fgetl(fid);
fclose(fid);
vec = sscanf(strrep(str,',','.'),'%f')
Alex Perrakis
Alex Perrakis el 10 de Mayo de 2022
Thanks man you're a champ

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Productos

Versión

R2019b

Preguntada:

el 5 de Mayo de 2022

Comentada:

el 10 de Mayo de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by