How to create an intermediate surface model from 2 Excel files with x, y and z cordinates where the number of rows in the matrices is different?
1 view (last 30 days)
Show older comments
I have two surface models represented by coordinates x, y, and z in excel files, which correspond to the endpoints of a cardiac cycle.
However, these models do not have the same number of rows in their matrices. I need to create an intermediate surface model that the output takes the form of a surface mesh and a stl file.
The eode that I'm using is:
% Load data from Excel files for Model 1 and Model 2
[nome,caminho]=uigetfile(".xlsx","Ficheiro");
data_model1=fullfile(caminho,nome);
[nome,caminho]=uigetfile(".xlsx","Ficheiro");
data_model2=fullfile(caminho,nome);
% Extract x, y, and z coordinates from each data set
model1_x = data_model1(:, 1);
model1_y = data_model1(:, 2);
model1_z = data_model1(:, 3);
model2_x = data_model2(:, 1);
model2_y = data_model2(:, 2);
model2_z = data_model2(:, 3);
% Interpolate data to have the same number of data points
num_points = max(numel(model1_x), numel(model2_x));
model1_x_interp = interp1(linspace(0, 1, numel(model1_x)), model1_x, linspace(0, 1, num_points));
model1_y_interp = interp1(linspace(0, 1, numel(model1_y)), model1_y, linspace(0, 1, num_points));
model1_z_interp = interp1(linspace(0, 1, numel(model1_z)), model1_z, linspace(0, 1, num_points));
model2_x_interp = interp1(linspace(0, 1, numel(model2_x)), model2_x, linspace(0, 1, num_points));
model2_y_interp = interp1(linspace(0, 1, numel(model2_y)), model2_y, linspace(0, 1, num_points));
model2_z_interp = interp1(linspace(0, 1, numel(model2_z)), model2_z, linspace(0, 1, num_points));
% Create the intermediate model's data points using barycentric interpolation
intermediate_x = (model1_x_interp + model2_x_interp) / 2;
intermediate_y = (model1_y_interp + model2_y_interp) / 2;
intermediate_z = (model1_z_interp + model2_z_interp) / 2;
% Triangulate the intermediate model's data points to create a surface mesh
DT = delaunayTriangulation(intermediate_x(:), intermediate_y(:), intermediate_z(:));
% Save the intermediate model's surface mesh to an STL file
stlwrite('intermediate_model.stl', DT.ConnectivityList, DT.Points);
% Plot the surface mesh
figure;
trisurf(DT.ConnectivityList, DT.Points(:, 1), DT.Points(:, 2), DT.Points(:, 3));
title('Surface Mesh of Intermediate Model');
xlabel('X');
ylabel('Y');
zlabel('Z');
However, an error appears:
">> Matlab_medio
Error using interp1
Wrong number of input arguments.
Error in Matlab_medio (line 20)
model1_x_interp = interp1(linspace(0, 1, numel(model1_x)), model1_x, linspace(0, 1, num_points));"
Can you help me?
>>
1 Comment
C B
on 24 Jul 2023
Can i know where have you read xls files?
i think you need to add readmatrix .
% Load data from Excel files for Model 1 and Model 2
[nome,caminho]=uigetfile(".xlsx","Ficheiro");
file_model1=fullfile(caminho,nome);
[nome,caminho]=uigetfile(".xlsx","Ficheiro");
file_model2=fullfile(caminho,nome);
data_model1 = readmatrix(file_model1); % Read data from Model 1 file
data_model2 = readmatrix(file_model2); % Read data from Model 2 file
% Extract x, y, and z coordinates from each data set
model1_x = data_model1(:, 1);
model1_y = data_model1(:, 2);
model1_z = data_model1(:, 3);
model2_x = data_model2(:, 1);
model2_y = data_model2(:, 2);
model2_z = data_model2(:, 3);
Accepted Answer
Voss
on 24 Jul 2023
data_model1 and data_model2 are character vectors containing the names of the files, not the contents of the files.
Therefore model1_x = data_model1(:, 1) contains the first character of the file name stored in data_model1, and model1_y is the second character, etc.
For example:
nome = 'some_file.xlsx';
caminho = 'C:\whatever';
data_model1=fullfile(caminho,nome);
model1_x = data_model1(:, 1)
model1_y = data_model1(:, 2)
model1_z = data_model1(:, 3)
So then when you pass model1_x to interp1, it gets confused because model1_x is a character where interp1 was expecting numeric data.
num_points = 10; % whatever
model1_x_interp = interp1(linspace(0, 1, numel(model1_x)), model1_x, linspace(0, 1, num_points));
To fix this, you need to read the data from the files, for instance using readmatrix:
[nome,caminho] = uigetfile(".xlsx","Ficheiro");
file_name1 = fullfile(caminho,nome);
data_model1 = readmatrix(file_name1);
[nome,caminho] = uigetfile(".xlsx","Ficheiro");
file_name2 = fullfile(caminho,nome);
data_model2 = readmatrix(file_name2);
% Extract x, y, and z coordinates from each data set
model1_x = data_model1(:, 1);
model1_y = data_model1(:, 2);
% etc.
0 Comments
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!