Challenge

Project - Data Handling and Interpolation

I have a set of data in excel that needed to be interpolated based on two variables called temperature and pressure. The code I have will read the interpolated data from one excel file(called testdata.xlsx), check the the temperature and pressure values in the excel file (results.xlsx)
The code will then place the interpolated answer in the third column of the excel sheet (results.xlsx). What I need is the ability to tell matlab which column the temperature comes from, which column the pressure comes from and which column to write the results in.
But on top of that I'd need it do this on all sheets as on my real results file there is a different temperature and pressure to calculate the interpolated data from. The columns temperature and pressure will be in will be the same for each sheet
Below are example excel files and current code
https://www.dropbox.com/s/0l6b1ngu1qszavj/results.xlsx?dl=0 https://www.dropbox.com/s/dbuzncyr5ie912y/testdata.xlsx?dl=0
if true
%%%%%%%
clear all
close all
fprintf('\n No excel files must be open when running the script')
fprintf('\n All files must be in current directory with the script File names \nare case sensitive and require extensions which are appropriate')
filename1=input('\n Enter the filename to read data from with\n extension (ex. testdata.xlsx) : ','s');
filename2=input('\n Enter the filename having values to which the \ndata is to be interpolated with extension (ex. results.xlsx ) : ','s');
data=xlsread(filename1);
xdata=data(1,2:end);
ydata=data(2:end,1);
V=data(2:end,2:end);
data2=xlsread(filename2);
xq=data2(:,1);
yq=data2(:,2);
str1=num2str(length(xq)+1);
rangeis=strcat('C2:C',str1);
intval=interp2(xdata,ydata,V,xq,yq);
xlswrite(filename2,intval,rangeis)
fprintf('\n Result written in third column of file %s (Re open to see it)(blank spaces means not a number )',filename2)
end
Key highlights
1.graphical solutions are more visual and appealing
%Simulataneous Equations
%3x1+2x2 =10
% 2x1 - x2 =2
%Graphical Solution
x= 0:0.1:10;
x1 = (1/3).*(18-2.*x);
x2= -2 + 2.*x;
plot(x,x1,'r')
hold on
plot (x,x2,'b');
grid on
xlabel('x1')
ylabel('x2')
Now I want to do a similar thing but for a set of equations with two variables instead of one. The equations are
I want them to plot like I could for above example and see their intersection. I know second equation is just a constant and hence i expected a plane passing through the value 5.
My attempt:
% Making an vector for input variable
x1_1= linspace(0,20);
y1_1=ones(1,100)*5;
%making a grid
[x1_1,y1_1] = meshgrid(x1_1,y1_1);
%The third equation of the set
z1_1= (1 + x1_1.*y1_1)./(x1_1+x1_1^2);
%making a vector for other plot
y1_2=ones(1,100)*5;
z1_2 = linspace(0,20);
%making a grid
[y1_2,z1_2] = meshgrid(y1_2,z1_2);
x1_2= 1+y1_2-z1_2;
% Plotting for 3D graph.
surf(x1_1,y1_1,z1_1);
hold on
surf(x1_2,y1_2,z1_2);
Interpolation of data:-
At the moment, I have this code - which works:

PTABLE = readtable('coordinates.dat');
POINTS = table2array(PTABLE);
VMRTABLE = readtable('values.dat');
VMR = table2array(VMRTABLE(:,2));
LON = POINTS(:,1);
LAT = POINTS(:,2);
VAL = VMR(:,1);
% Convert coordinates to radians
LATr = LAT*pi/180;
LONr = LON*pi/180;
% Create a longitude/latitude grid with 0.1 degree spacing
nrows = 1800;
ncols = 3600;
X = linspace(0,360,ncols);
Y = linspace(-90,90,nrows);
[lon, lat] = meshgrid(X,Y);
% Convert to radians and make longitude measured "westwards" (-ve sign)
lon = -lon*pi/180;
lat = lat*pi/180;
% Convert the coordinate data points to cartesian
[x, y, z] = sph2cart(LONr,LATr,1);
% Convert the gridded points to cartesian
[xg, yg, zg] = sph2cart(lon,lat,0.998);
% Perform the interpolation, with no extrapolation
F=scatteredInterpolant(x,y,z,VAL,'linear','none');
vq = F(xg,yg,zg);
save('data_interp.dat', 'vq', '-ascii'
Read excel file:-
Take advantage of the fact that excel files are actually zip files UNZIP and import the matlab excel file

Read Excel file with Matlab

Here are a couple of thoughts if you are trying to read the image data into MATLAB:
  • Take advantage of the fact that Excel files are actually ZIP files. Unzip and import the images.
For instance, I made a file with two pictures. You'll see them in the xl/media file:
>> contents = string(unzip('twopictures.xlsx'))'
contents =
14×1 string array
"[Content_Types].xml"
"_rels/.rels"
"xl/_rels/workbook.xml.rels"
"xl/workbook.xml"
"xl/drawings/drawing1.xml"
"xl/media/image1.jpeg"
"xl/styles.xml"
"xl/drawings/_rels/drawing1.xml.rels"
"xl/media/image2.jpeg"
"xl/theme/theme1.xml"
"xl/worksheets/sheet1.xml"
"xl/worksheets/_rels/sheet1.xml.rels"
"docProps/core.xml"
"docProps/app.xml"
I could then use imread to read the images, e.g.
im = imread('xl/media/image1.jpeg');
Write to text file:-
if N_Locations > 1
if length(char(ReadSKU))> 8
%simulation cannot handle file names longer than 8
%characters and those were replace to names with 'temp':
newname = strcat('temp',num2str(LongName))
LongName = LongName+1;
try
fid = fopen(['C:\TdKSimulatie\Stocksim\' char(newname) '.sim']);
C = textscan(fid, '%s', 'delimiter', '\n');%,'HeaderLines',5);
catch
NotFound = 1;
% ME
% idSegLast = regexp(ME1.identifier, '(?<=:)\w+$', 'match');
% fprintf('Unable to access file %s\n', filename);
% rethrow(ME2)
end
else
try
fid = fopen(['C:\TdKSimulatie\Stocksim\' char(ReadSKU) '.sim']);
C = textscan(fid, '%s', 'delimiter', '\n');%,'HeaderLines',5);
catch
NotFound=1;
% ME2
% idSegLast = regexp(ME1.identifier, '(?<=:)\w+$', 'match');
% fprintf('Unable to access file %s\n', filename);
% rethrow(ME2)
end
end
if NotFound == 0
%HERE I MAKE SEVERAL CHANGES TO THE DATA.
%HERE I MAKE SEVERAL CHANGES TO THE DATA.
%HERE I MAKE SEVERAL CHANGES TO THE DATA.
%HERE I MAKE SEVERAL CHANGES TO THE DATA.
end
% write adaptations back to .sim file:
%fclose(fid);
%fid = fopen(['C:\TdKSimulatie\Stocksim\test.txt','w']);
fprintf(fid,'%s\n',C_new{:});
fclose(fid);
Deliverables:-
Fine missing data and save text file:-
The only operating system that MATLAB has ever run on that supported that ability was DEC's VMS, and for technical reasons VMS's facility for that could not be used with MATLAB.
The modern treatment of "lines" as being delimited by a particular character or character pair (e.g., LF or CR+LF) does not offer any way to count the lines short of reading through the file and counting the delimiters.
function n = linecount(fid)
n = 0;
tline = fgetl(fid);
while ischar(tline)
tline = fgetl(fid);
n = n+1;
end
Interpolate the data set:-
i have found the answer myself. The poly function takes arguments as roots of a polynomial. Like if x-2=0 is the equation, poly(2) is enough to find the polynomial matrix.
So, we dont need to put extra 'x' in poly. Except this problem, the code is alright. Now using polyval function we can plot smooth curve from limited data.
x=[0 1 2 3 4 5 6];
y=[0 .8415 .9093 .1411 -.7568 -.9589 -.2794];
sum=0;
for i=1:length(x)
p=1;
for j=1:length(x)
if j~=i
c = poly(x(j))/(x(i)-x(j));
p = conv(p,c);
end
end
term = p*y(i);
sum= sum + term;
end
disp(sum);
Save your program and attach the same:-
In this case, you can add the configuration file within the "Files installed for your end user" section. This will have the installer install the file alongside with the executable itself (without including it in the CTF).
Depending on how you are referencing the configuration file in the call to load, you may want to add %#exclude to make sure the file is not included in the CTF. For example:
- If you are calling "load foo" or "load foo.mat" it will get included in the CTF without the %#exclude. So in this case, you should add the %#exclude
- However, if you are calling "load ./foo.mat", you would not need to add the %#exclude
Save text file and upload in attachment:-

You can export tabular data from MATLAB® workspace into a text file using the writetable function. Create a sample table, write the table to text file, and then write the table to text file with additional options.

Create a sample table, T, containing the variables Pitch, Shape, Price and Stock.

Pitch = [0.7;0.8;1;1.25;1.5];
Shape = {'Pan';'Round';'Button';'Pan';'Round'};
Price = [10.0;13.59;10.50;12.00;16.69];
Stock = [376;502;465;1091;562];
T = table(Pitch,Shape,Price,Stock)
T=5×4 table
    Pitch      Shape       Price    Stock
    _____    __________    _____    _____

     0.7     {'Pan'   }       10     376 
     0.8     {'Round' }    13.59     502 
       1     {'Button'}     10.5     465 
    1.25     {'Pan'   }       12    1091 
     1.5     {'Round' }    16.69     562 

Export the table, T, to a text file named tabledata.txt. View the contents of the file. By default, writetable writes comma-separated data, includes table variable names as column headings.

writetable(T,'tabledata.txt');
type tabledata.txt
Pitch,Shape,Price,Stock
0.7,Pan,10,376
0.8,Round,13.59,502
1,Button,10.5,465
1.25,Pan,12,1091
1.5,Round,16.69,562

Create a table T2 which includes row names using the RowNames name-value pair argument.

rowNames = {'M4';'M5';'M6';'M8';'M10'};
T2 = table(Pitch,Shape,Price,Stock,'RowNames',rowNames)
T2=5×4 table
           Pitch      Shape       Price    Stock
           _____    __________    _____    _____

    M4      0.7     {'Pan'   }       10     376 
    M5      0.8     {'Round' }    13.59     502 
    M6        1     {'Button'}     10.5     465 
    M8     1.25     {'Pan'   }       12    1091 
    M10     1.5     {'Round' }    16.69     562 

Export T2 to a tab-delimited text file named tabledata2.txt. Use the Delimiter name-value pair argument to specify a tab delimiter, and the WriteRowNames name-value pair argument to include row names. View the contents of the file.

writetable(T2,'tabledata2.txt','Delimiter','\t','WriteRowNames',true);
type tabledata2.txt
Row	Pitch	Shape	Price	Stock
M4	0.7	Pan	10	376
M5	0.8	Round	13.59	502
M6	1	Button	10.5	465
M8	1.25	Pan	12	1091
M10	1.5	Round	16.69	562

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.