Extract Values from String with sscanf

5 views (last 30 days)
Alex Perrakis
Alex Perrakis on 5 May 2022
Commented: Alex Perrakis on 10 May 2022
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 Comments
Jan
Jan on 10 May 2022
@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')

Sign in to comment.

Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!