MATLAB Answers

How to save variables from function output to workspace?

79 views (last 30 days)
Simone Frauenfelder
Simone Frauenfelder on 2 May 2017
function [ A, B] = GetText(filename)
fid = fopen(filename,'r');
x = textscan(fid,'%n%s','headerlines',1);
A = x(:,1);
B = x(:,2);
When I run this function the output is ans = "the data in the 1st column" and ans = "data in the 2nd column" and ans = [108x1 double] but the variables 'A' and 'B' are not saved to the workspace. How to I fix the function so the answers are defined as A and B and the variables are saved to the workspace?

  1 Comment

Stephen Cobeldick
Stephen Cobeldick on 3 May 2017
@Simone Frauenfelder: today I formatted your code correctly for you. Next time you can do it yourself: select the code text, then click the {} Code button above the textbox.

Sign in to comment.

Answers (1)

Alessandro on 2 May 2017
Edited: Alessandro on 2 May 2017
You are probably calling the function in a wrong way from your script/command line.
[A,B] = GetText(filename);
will save both variables A and B in the workspace.


Sign in to comment.