Clear Filters
Clear Filters

My function to read data from three csv files is only reading one file and storing it in 'ans' instead of the variable K.

3 views (last 30 days)
I am trying to use a function to get data from three csv files and store the data in three variables. When I run the code, it reads the data from 'K_matrix' and stores it in 'ans' then stops. I have checked to make sure that the formatting for the file path is correct as when I remove the function portion and just have the "variable = readmatrix();" part of the code, it returns three variables with the correct matrcies and data. Please let me know if there is any additional information I should provide and thank you!
function [K, K_sub, Q] = inputData()
K = readmatrix('K_matrix.csv');
K_sub = readmatrix('K_submatrix.csv');
Q = readmatrix('Q_subvector.csv');
end
Here is the output as well (This is only K_matrix.csv):
MATLAB_PROJECT_Version_1
ans =
1.0e+08 *
Columns 1 through 14
0.0076 0 -0.0795 -0.0076 0 -0.0795 0 0 0 0 0 0 0 0
0 3.2258 0 0 -3.2258 0 0 0 0 0 0 0 0 0
-0.0795 0 1.1131 0.0795 0 0.5566 0 0 0 0 0 0 0 0
-0.0076 0 0.0795 0.3737 0.0575 0.0792 -0.3661 -0.0575 -0.0003 0 0 0 0 0
0 -3.2258 0 0.0575 3.2350 0.0020 -0.0575 -0.0092 0.0020 0 0 0 0 0
-0.0795 0 0.5566 0.0792 0.0020 1.1611 0.0003 -0.0020 0.0240 0 0 0 0 0
0 0 0 -0.3661 -0.0575 0.0003 0.7322 0 0.0006 -0.3661 0.0575 0.0003 0 0
0 0 0 -0.0575 -0.0092 -0.0020 0 0.0183 0 0.0575 -0.0092 0.0020 0 0
0 0 0 -0.0003 0.0020 0.0240 0.0006 0 0.0959 -0.0003 -0.0020 0.0240 0 0
0 0 0 0 0 0 -0.3661 0.0575 -0.0003 0.3666 -0.0575 0.0055 -0.0006 0
0 0 0 0 0 0 0.0575 -0.0092 -0.0020 -0.0575 0.6420 -0.0020 0 -0.6329
0 0 0 0 0 0 0.0003 0.0020 0.0240 0.0055 -0.0020 0.1288 -0.0058 0
0 0 0 0 0 0 0 0 0 -0.0006 0 -0.0058 0.0006 0
0 0 0 0 0 0 0 0 0 0 -0.6329 0 0 0.6329
0 0 0 0 0 0 0 0 0 0.0058 0 0.0404 -0.0058 0
Column 15
0
0
0
0
0
0
0
0
0
0.0058
0
0.0404
-0.0058
0
0.0809

Accepted Answer

Image Analyst
Image Analyst on 4 Dec 2023
Edited: Image Analyst on 4 Dec 2023
How did you run it? You didn't just push the green run triangle did you? Did you do something like this:
[K, K_sub, Q] = inputData() % Test code
%========================================================================
% Function definition (could be in the same m-file or a different m-file.
function [K, K_sub, Q] = inputData()
K = readmatrix('K_matrix.csv');
K_sub = readmatrix('K_submatrix.csv');
Q = readmatrix('Q_subvector.csv');
end
If so, nothing should go into ans and it should go into the 3 matrixes.
  2 Comments
JD
JD on 4 Dec 2023
I'm sorry that is what I was doing! I'm new to using MATLAB so sorry about that. I just used the test code and it works perfectly. Thank you so much!
Image Analyst
Image Analyst on 4 Dec 2023
You're welcome. With function that return several output variables, if you don't accept the results into multiple variables, it will return only the first one that is defined on the "function" line. The others are just tossed out and not returned.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 4 Dec 2023
[K, K_sub, Q] = inputData();
You have to store the outputs into variables.
When you invoke a matlab Function with named outputs, then MATLAB does not automatically store values in the corresponding variable names in the calling environment. MATLAB outputs are purely positional. So for example,
function [K, K_sub, Q] = inputData()
does not tell the function to store into K in the calling environment: instead it is convenying, "We will use K as a local nick-name for whatever output variable the user gives in the first output position when they call the function.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!