Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I have made this matlab code which reads and prints the data from . dat files:function data = readData ( filename ) filename = input
I have made this matlab code which reads and prints the data from dat files:function data readDatafilename
filenameinputEnter file name: s;
fid fopenfilenamer;
if fid
errorError opening file filename;
end
data struct;
while ~feoffid
line fgetlfid;
if isemptyline
continue;
end
Split the line into parameter and value
parameter value strtokline:;
Remove the colon and whitespace from the value
value strtrimstrrepvalue:;
Remove comments lines starting with #
value strtokvalue#;
Convert the value to a number if it is not empty
if ~isemptyvalue
numValue strdoublevalue;
Check if the conversion was successful
if ~isnannumValue
data.strtrimparameter numValue;
else
warningCould not convert value for parameter parameter to a number.;
end
else
warningEmpty value for parameter parameter ;
end
end
fclosefid;
end
how can I make it more similar to my proffesors way which is based on another exercise:function v iline getlabelrealsfr fn iline, lab, n icod
Diabase n pragmatikoys ariqmoys; an icod prepei na einai qetikoi.
dline iline fgetlcfr iline;
if dline
errorUnexpected end of file s after line d: label s was
expected', fn iline, lab;
end
dl strsplitstrtrimdline:;
if ~strcmpdl lab
errorError at line d of file s: label s was expected', iline, fn
lab;
elseif lengthdl
errorError at line d of file s: d real numbers were expected',
iline, fn n;
end
dline dl;
v strnumdline;
if lengthv n;
errorError at line d of file s: d real numbers were expected',
iline, fn n;
end
if icod && anyv
errorError at line d of file s: d positive real numbers were
expected', iline, fn n;
end
end
clear; clc; close all;
while true
pref inputEnter file prefix: s;
fn pref pro'; Complete filename: prefixsuffix
fr fopenfnr;
if fr
break
end
fprintfCan not findopen s Please try again.
fn;
end
iline ;
temp iline getlabelrealsfn fr iline, 'ELASTIC CONSTANTS:;
E temp
v temp
g iline getlabelrealsfn fr iline, 'SPECIFIC WEIGHT:;
g
fclosefr;
the file on my profs example:ELASTIC CONSTANTS: #kNm dimensionless
SPECIFIC WEIGHT: #kNm
my files :MANNING:
DIAMETER: #m
SLOPE: # per cent actual slope is
DEPTH: #m
DISCHARGE: #msec
and:
#Petaloid section
DIAMETER: #m
MANNING:
SLOPE: # per cent actual slope is
#DEPTH: #m
DISCHARGE: #msec
thanks MATLAB CODE ONLY!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started