Getting Values from Excel Table on Matlab - matlab

I've been trying to simulate a chemical reactor on Matlab. For this to work I need to be able to get some values from excel tables with thermodynamic properties. Each table is for a chemical species and the first row is always the temperature.
I would like to input the temperature so that Matlab would search the temperature column for the value and return, for example, the associated enthalpy.
I used xlsread to load the tables as matrices but I am having trouble to tell Matlab what to do next.
I've been told to try
[row, ~] = find(temp<=data(:,2)); %temp is the given temperature
but this just returns me
row = 1, 2, ...
I would also like to be able to handle intermediate temperatures that are not explicit in the table, it could be some kind of interpolation or just getting the nearest value.
Any help will be truly appreciated.

Related

Pairwise Similarity and Sorting Samples

The following is a problem from an assignment that I am trying to solve:
Visualization of similarity matrix. Represent every sample with a four-dimension vector (sepal length, sepal width, petal length, petal width). For every two samples, compute their pair-wise similarity. You may do so using the Euclidean distance or other metrics. This leads to a similarity matrix where the element (i,j) stores the similarity between samples i and j. Please sort all samples so that samples from the same category appear together. Visualize the matrix using the function imagesc() or any other function.
Here is the code I have written so far:
load('iris.mat'); % create a table of the data
iris.Properties.VariableNames = {'Sepal_Length' 'Sepal_Width' 'Petal_Length' 'Petal_Width' 'Class'}; % change the variable names to their actual meaning
iris_copy = iris(1:150,{'Sepal_Length' 'Sepal_Width' 'Petal_Length' 'Petal_Width'}); % make a copy of the (numerical) features of the table
iris_distance = table2array(iris_copy); % convert the table to an array
% pairwise similarity
D = pdist(iris_distance); % calculate the Euclidean distance and store the result in D
W = squareform(D); % convert to squareform
figure()
imagesc(W); % visualize the matrix
Now, I think I've got the coding mostly right to answer the question. My issue is how to sort all the samples so that samples from the same category appear together because I got rid of the names when I created the copy. Is it already sorted by converting to squareform? Other suggestions? Thank you!
It should be in the same order as the original data. While you could sort it afterwards, the easiest solution is to actually sort your data by class after line 2 and before line 3.
load('iris.mat'); % create a table of the data
iris.Properties.VariableNames = {'Sepal_Length' 'Sepal_Width' 'Petal_Length' 'Petal_Width' 'Class'}; % change the variable names to their actual meaning
% Sort the table here on the "Class" attribute. Don't forget to change the table name
% in the next line too if you need to.
iris_copy = iris(1:150,{'Sepal_Length' 'Sepal_Width' 'Petal_Length' 'Petal_Width'}); % make a copy of the (numerical) features of the table
Consider using sortrows:
tblB = sortrows(tblA,'RowNames') sorts a table based on its row names. Row names of a table label the rows along the first dimension of the table. If tblA does not have row names, that is, if tblA.Properties.RowNames is empty, then sortrows returns tblA.

How to apply tsne() to MATLAB tabular data?

I have a 33000 x 1975 table in MATLAB, obviously requiring dimensionality reduction before I do any further analysis. The features are the 1975 columns and the rows are instances of the data. I tried using tsne() function on the MATLAB table, but it seems tsne() only works on numeric arrays. The thing is that is there a way to apply tsne on my MATLAB table. The table consists of both numeric as well as string data types, so table2array() doesn't work in my case for converting the table to a numeric array.
Moreover, it seems from the MATHWORKS documentation, as applied to the fisheriris dataset as an example, that tsne() takes the feature columns as the function argument. So, I would need to separate the predictors from the resonses, which shouldn't be a problem. But, initially, it seems confusing as to how I can proceed further for using the tsne. Any suggestions in this regard would be highly appreciated.
You can probably use table indexing using {} to get out the data that you want. Here's a simple example adapted from the tsne reference page:
load fisheriris
% Make a table where the first variable is the species name,
% and the other variables are the measurements
data = table(species, meas(:,1), meas(:,2), meas(:,3), meas(:,4))
% Use {} indexing on 'data' to extract a numeric matrix, then
% call 'tsne' on that
Y = tsne(data{:, 2:end});
% plot as per example.
gscatter(Y(:,1),Y(:,2),data.species)

How to use 3D surface data from cftool in Simulink lookup table?

I am designing a battery model with an internal resistance which is dependant on two variables: SoC and temperature.
I have interpolated the data I have (x,y and z basically - a total of 131 points each) with MATLAB's curve fitting toolbox and was able to generate the desired 3D map of that dependence (see the picture below):
My question is how can I use that map now for my Simulink model? As input parameters I will have SoC and temperature and the resistance in ohm should be the output. However, I have not been able to find a convenient way to export the data in a suitable lookup table (or similarly useful, my first guess was that I should use a 2-D lookup table in this case) in Simulink. However, I am quite new to this and I do not know how to generate the the table data for the Simulink LUT.
Simulink LUT:
Table data is your interpolated z-data from curve fitting. I guess it will have a value for every combination of breakpoints (i.e. it covers every grid intersection in your first diagram). So if Breakpoint 1 is 100 elements and Breakpoint 2 is 40 elements, Table data is 100x40.
If you can't get the data out from the GUI-based interactive curve fit, I guess you can extract the data from the command line. The following is an excerpt of Mathworks' curve fitting documentation. It would be good to verify this because I don't have the toolbox to test it though.
•Interpolation: fittedmodel = fit([Time,Temperature], Energy, 'cubicinterp');
•Evaluation: fittedmodel(80, 40)
Based on your LUT inputs u1 and u2, the table will interpolate or extrapolate the grid to get your output value.
Hope that helps.
I did find a solution after all, thanks Tom for your help, the fittedmodel() function was indeed the key of it. I then used two FOR loops to populate my matrix which was 49x51 (as seen by the grid in the image) after the cftool interpolation. After that it was all a matter of two for loops in one another to populate my matrix with the z values of my T and SoC parameters.
for x = 1:49
for y = 1:51
TableData(x,y)=fittedmodel(B_SoC(x),B_Temp(y));
end
end
Where TableData is the 49x51 matrix required for my LUT, B_SoC and B_Temp being [0:2.083:100] and [-10:1.1:45] respectively (determined as the desired start and end of my x and y axis with the spacing taken from the image with the data cursor).

multiple index integer linear program

I have a bounded integer linear programming which has more than 1000 decision variables. the decision variable has five indices x_{ijkzf} the constraints have one or two or more summations. Are there any easy trick for extracting, constructing and preparing the coefficient matrix? it is not easy to construct the matrices for integer linear program function.
It can be done just using GAMS. We can read data from Excel in GAMS. Also the multiple-dimension matrices can easily be read in GAMS. Using Table command, we can import a table to GAMS. Also, we can use excel file. For a multidimensional table, we can add indices hierarchically in excel. For example, for A(i,j,k), we can add i values in first ow of excel sheet. For i=1 we can enter A(1,j,k) at under i=1 cell as a 2d matrix. For i=2, we can enter A(2,j,k) under i=2 in excel sheet.

Matlab using Interp1 for interpolation of matrx data set

I have a large data set of offline and online data, offline data is only taken every two hourly so wish to interpolate between the gaps. where the data is missing it is replaced with -9.999. I wish to interpolate in order to estimate these values. My idea is to find the missing values in the set to compare against the time intervals, but I cannot get it to work
This is what I have so far:
iv = 33; % column which holds cell weight
ind = find (Data(:,iv)<0); % find the indices of missing values
Interp_iv = interp1 (Data(ind,2),Data(ind,2),Data(:,2),'spline')
Your x and v is the same try this:
interp1 (Data(ind,2),Data(ind,iv),Data(:,2),'spline')