Getting an array from another function

5 Ansichten (letzte 30 Tage)
Patrick Lydon
Patrick Lydon am 11 Jul. 2017
Kommentiert: Star Strider am 11 Jul. 2017
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.

Akzeptierte Antwort

Star Strider
Star Strider am 11 Jul. 2017
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 Kommentare
Patrick Lydon
Patrick Lydon am 11 Jul. 2017
Thank you very much, it works great!
Star Strider
Star Strider am 11 Jul. 2017
My pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Guillaume
Guillaume am 11 Jul. 2017
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

Community Treasure Hunt

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

Start Hunting!

Translated by