How to save variables from output of function as vector?

조회 수: 25 (최근 30일)
Simone Frauenfelder
Simone Frauenfelder 2017년 5월 3일
편집: Walter Roberson 2017년 5월 4일
function [ A, B] = GetText(filename)
fid = fopen(filename,'r');
x = textscan(fid,'%n%s','headerlines',1);
fclose(fid);
A = x(:,1);
B = x(:,2);
B{1}
A{1}
end
when I use this function
>> [A,B] = GetText(filename)
the output is A and B are cells. I need to use A and B for input to other functions so how do I make A and B save as vectors in the workspace?
thank you

답변 (2개)

Jan
Jan 2017년 5월 3일
편집: Jan 2017년 5월 3일
Perhaps like this:
function [A, B] = GetText(filename)
fid = fopen(filename, 'r');
x = textscan(fid,'%n%s','headerlines',1);
fclose(fid);
A = x{1, 1};
B = x{1, 2}; % Typo, was x{2,2}
end
But perhaps "A and B save as vectors" might mean something else. Please post the contents of the variables, or the file, or explain what you expect as output exactly.
  댓글 수: 2
Simone Frauenfelder
Simone Frauenfelder 2017년 5월 3일
This gave me a dimension error
Jan
Jan 2017년 5월 3일
편집: Jan 2017년 5월 3일
Please do not only mention that there is an error, but post the complete error message. There was a typo in my code. Perhaps this caused the error. But most of all: please answer the questions for clarifications. This is your problem and I want to help you, but to do this, it is required to know, what the code is reading and what you want as output. Ignoring questions for clarifications is not useful, if you want others to help you.

댓글을 달려면 로그인하십시오.


Stephen23
Stephen23 2017년 5월 3일
function [ A, B] = GetText(filename)
fid = fopen(filename,'r');
x = textscan(fid,'%n%s','headerlines',1);
fclose(fid);
A = x{1};
B = x{2};
end
  댓글 수: 2
Simone Frauenfelder
Simone Frauenfelder 2017년 5월 3일
I tried this and got A saved as a double (YAY) but B is still saved as a cell. Do you know to fix this?
Stephen23
Stephen23 2017년 5월 3일
편집: Stephen23 2017년 5월 3일
@Simone Frauenfelder: you specified the second textscan formatSpec to be %s, which according to the documentation "Read as a cell array of character vectors." The variable B is a cell array of character vectors. How do you imagine that character vectors should be, if not in a cell array?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by