Getting an array from another function

조회 수: 5 (최근 30일)
Patrick Lydon
Patrick Lydon 2017년 7월 11일
댓글: Star Strider 2017년 7월 11일
Basically, I have a function called read_tsf which reads a time stamp file that you choose and puts it into a 48x1 matrix. I am currently writing a program that analyzes data and plots it accordingly. I need to use the time stamp file matrix in this code to mark points on the plot. I tried just calling the function in the code "read_tsf;" and then tried retrieving the matrix later in the code "tsf_dbl();" but it gives me an error saying "Undefined function or variable 'tsf_dbl'." So I am obviously doing this wrong. How do I use my function read_tsf in order to retrieve the matrix and use it in another program? Thank you.

채택된 답변

Star Strider
Star Strider 2017년 7월 11일
You did not say how you are calling either ‘read_tsf’ or ‘tsf_dbl’. Functions have their own workspaces that they do not share with the base workspace. Your ‘read_tsf’ function must output the vector to the base workspace in order for other functions to use it.
Try something like this:
ts_vct = read_tsf(filename);
ts_num = tsf_dbl(ts_vct);
  댓글 수: 10
Patrick Lydon
Patrick Lydon 2017년 7월 11일
Thank you very much, it works great!
Star Strider
Star Strider 2017년 7월 11일
My pleasure!

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

추가 답변 (1개)

Guillaume
Guillaume 2017년 7월 11일
That function is really not well written. Here is a better version with all the useless operations removed:
function timestamps = read_tsf(filepath)
%Imports & parses data from BINARY TimeFile
%filepath: full path of file to read. If not specified or empty, the function prompts for a file
if nargin == 0 || isempty(filepath)
[filename, path] = uigetfile({'*.tsf', 'All Files (*.tsf)'}, 'Choose a Binary Data File');
if filename == 0
error('operation cancelled by user');
end
filepath = fullfile(path, filename);
end
fid = fopen(filepath, 'r', 'l');
if fid == -1
error('Failed to open file %s', filepath);
end
timestamps = fread(fid, Inf, 'double');
fclose(fid);
end
To use that function you have to give it an output. e.g. in your code:
tsf_dbl = read_tsf; %function will prompt for file
%or
tsf_dbl = read_tsf('C:\somewhere\somefile') %function will read specified file

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by