what I write to a file is different to what I read back from the same file

8 views (last 30 days)
I calculate calibration constants using a simple program as shown below for two variables. The input data is coded into a table.
% Set up calibration of EXP from Table
close all;
clear all
format SHORTE
clc
ExpNumber=input('Enter Experiment Number = ')
CAL_Path='c:\UNSW\RESEARCH_ACTIVE\BP_Research\BP_UP_DOWN_INVASIVE_NEW\Data_CAL\';
CAL_File=[CAL_Path 'CAL_EXP_' num2str(ExpNumber) '.mat']
%==========================================================================
% Enter Calibration Data into matrix below
Pressure=[0 44.2 91 139.2 186 242.4]
Count_cp=[168 12002 24002 36005 48008 62008];
Count_iabp=[2206 12124 22825 33549 44318 57075];
Calibration=table(Pressure, Count_cp, Count_iabp)
disp(ExpNumber)
disp([Calibration.Pressure' Calibration.Count_cp' Calibration.Count_iabp'])
%==========================================================================
% First for CP
x=Calibration.Count_cp';
y=Calibration.Pressure';
[f,q] = fit(x,y,'poly1')
figure(10)
plot(f,x,y)
% Now for IABP
X=Calibration.Count_iabp';
[F,Q]=fit(X,y,'poly1')
figure(20)
plot(F,X,y)
cp_cal=[f.p1 f.p2];
iabp_cal=[F.p1 F.p2];
save(CAL_File, 'ExpNumber','Calibration','cp_cal','f','q','iabp_cal','F','Q')
clear ExpNumber Calibration cp_cal f q iabp_cal F Q % Should not be necessary as I clear all variable on start of program
disp('Ended normally ***********')
This produces a file CAL_EXP_10, as the ExpNumber entered is 10
I then wrote a simple program to check the data written by reading it back, using the simple program, ReadCalFile.m
% Read CAL file
clear all
clc
Exp=input('Enter Experiment Number = ') % Required calibration data to read
CAL_Path='c:\UNSW\RESEARCH_ACTIVE\BP_Research\BP_UP_DOWN_INVASIVE_NEW\Data_CAL\';
CAL_File=[CAL_Path 'CAL_EXP_' num2str(Exp) '.mat']
load CAL_File
who
disp(Exp)
disp(ExpNumber)
disp([Calibration.Pressure' Calibration.Count_cp' Calibration.Count_iabp'])
%---------------------------------------------------------------------------------------------------------end of read program
The data read back when Exp is selcted as 10 returns data fro CAL_EXP_12.mat as shown below,
Enter Experiment Number = 10
Exp =
10 % Number of requested cal file to read
CAL_File =
'c:\UNSW\RESEARCH_ACTIVE\BP_Research\BP_UP_DOWN_INVASIVE_NEW\Data_CAL\CAL_EXP_10.mat'
Your variables are:
CAL_File Calibration ExpNumber Q f q
CAL_Path Exp F cp_cal iabp_cal
10 % Correct file requested
12 % Incorrect ExpNumber returned
0 1.6700e+02 6.2500e+02 % Data matrix returned does not match data for EXP_10
4.3200e+01 1.2006e+04 1.0620e+04
9.1700e+01 2.4010e+04 2.1303e+04
1.4000e+02 3.6015e+04 3.2152e+04
1.8710e+02 4.8009e+04 4.3059e+04
2.4160e+02 6.2001e+04 5.5854e+04

Accepted Answer

Stephen23
Stephen23 on 21 Oct 2024
Edited: Stephen23 on 21 Oct 2024
Because you are reading a different file than you are writing. The main problem is this line here
load CAL_File
where you LOAD a file literally named "CAL_File" using command syntax (you should have used function syntax):
After fixing that and some other "features" of your code and testing it here:
ExpNumber = 10;
CAL_Path = '.';
CAL_File = fullfile(CAL_Path,['CAL_EXP_',num2str(ExpNumber),'.mat']);
% Enter Calibration Data into matrix below
Pressure = [ 0; 44.2; 91; 139.2; 186;242.4];
Count_cp = [168;12002;24002; 36005;48008;62008];
Count_iabp = [2206;12124;22825;33549;44318;57075];
% First for CP
[f,q] = fit(Count_cp,Pressure,'poly1');
% Now for IABP
[F,Q] = fit(Count_iabp,Pressure,'poly1');
cp_cal = [f.p1,f.p2];
iabp_cal = [F.p1,F.p2];
save(CAL_File, 'ExpNumber','cp_cal','f','q','iabp_cal','F','Q')
clear all
ExpNum = 10;
CAL_Path = '.';
CAL_File = fullfile(CAL_Path,['CAL_EXP_',num2str(ExpNum),'.mat']);
load(CAL_File)
who
Your variables are: CAL_File CAL_Path ExpNum ExpNumber F Q cp_cal f iabp_cal q
disp(ExpNum)
10
disp(ExpNumber)
10
Tips:
  1 Comment
Branko Celler
Branko Celler on 21 Oct 2024
Thank you! This was a subtlety that escaped me and caused me a lot of time wasting. I will read the advisory URLs you have sent me!

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!