Answered step by step
Verified Expert Solution
Link Copied!

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 = readData(filename)
filename=input('Enter file name: ','s');
fid = fopen(filename,'r');
if fid ==-1
error(['Error opening file ' filename]);
end
data = struct();
while ~feof(fid)
line = fgetl(fid);
if isempty(line)
continue;
end
% Split the line into parameter and value
[parameter, value]= strtok(line,':');
% Remove the colon and whitespace from the value
value = strtrim(strrep(value,':',''));
% Remove comments (lines starting with '#')
value = strtok(value,'#');
% Convert the value to a number if it is not empty
if ~isempty(value)
numValue = str2double(value);
% Check if the conversion was successful
if ~isnan(numValue)
data.(strtrim(parameter))= numValue;
else
warning(['Could not convert value for parameter ' parameter ' to a number.']);
end
else
warning(['Empty value for parameter ' parameter '.']);
end
end
fclose(fid);
end
how can I make it more similar to my proffesors way which is ( based on another exercise):function [v, iline]= getlabelreals9(fr, fn, iline, lab, n, icod)
%Diabase n pragmatikoys ariqmoys; an icod ==1 prepei na einai qetikoi.
[dline, iline]= fgetlc(fr, iline);
if dline ==-1
error('Unexpected end of file %s after line %d: label "%s" was
expected', fn, iline, lab);
end
dl = strsplit(strtrim(dline),':');
if ~strcmp(dl{1}, lab)
error('Error at line %d of file %s: label "%s" was expected', iline, fn,
lab);
elseif length(dl)<2
error('Error at line %d of file %s: %d real numbers were expected',
iline, fn, n);
end
dline = dl{2};
v = str2num(dline);
if length(v)< n;
error('Error at line %d of file %s: %d real numbers were expected',
iline, fn, n);
end
if icod ==1 && any(v <=0)
error('Error at line %d of file %s: %d positive real numbers were
expected', iline, fn, n);
end
end
clear; clc; close all;
while true
pref = input('Enter file prefix: ','s');
fn =[pref '.pro']; %Complete filename: prefix+suffix
fr = fopen(fn,'r');
if fr >0
break
end
fprintf('Can not find/open %s. Please try again.
', fn);
end
iline =0;
[temp, iline]= getlabelreals(fn, fr, iline, 'ELASTIC CONSTANTS:',2,1);
E = temp(1)
v = temp(2)
[g, iline]= getlabelreals(fn, fr, iline, 'SPECIFIC WEIGHT:',1,1);
g
fclose(fr);
the file on my profs example:ELASTIC CONSTANTS: 280000000.10 #kN/m2, dimensionless
SPECIFIC WEIGHT: 25 #kN/m3
!my files :MANNING: 0.016
DIAMETER: 1.0 #m
SLOPE: 0.5 # per cent (actual slope is 0.005)
DEPTH1: 0.4 #m
DISCHARGE1: 0.6 #m3/sec
and:
#Petaloid section
DIAMETER: 1.0 #m
MANNING: 0.016
SLOPE: 0.5 # per cent (actual slope is 0.005)
#DEPTH1: 0.4 #m
DISCHARGE1: 0.6 #m3/sec
thanks !MATLAB CODE ONLY!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database In Depth Relational Theory For Practitioners

Authors: C.J. Date

1st Edition

0596100124, 978-0596100124

More Books

Students also viewed these Databases questions

Question

Describe the appropriate use of supplementary parts of a letter.

Answered: 1 week ago