How do I make an output variable from a sub-routine function a variable in the main function/script? I have a function that reads data from a .txt file (data for output variable) and I want that data in my main file. Is there something I'm missing?
Mostrar comentarios más antiguos
function [ Output ] = readData(inputvariable= opened .txt file)
data = fscanf( opened.txt, '%f', 4);
return
end
Respuestas (1)
Star Strider
el 26 de Jul. de 2015
Without knowing more than you stated in your Question, I would code it (passing the argument) as :
function [ data ] = readData(inputvariable)
data = fscanf( inputvariable, '%f', 4);
return
end
Your ‘inputvariable’ argument is the file identification (’fid’) number of the file you already opened.
I would pass the filename and let the function open, read, and close the file, but you may have your own reasons for passing the file id to your function instead.
2 comentarios
Michael Polewski
el 26 de Jul. de 2015
Star Strider
el 26 de Jul. de 2015
You don’t need to set ‘data’ as a global variable. Just create it as an output (returned value) from your function, do not overwrite it in any subsequent assignments, and use it wherever you want.
Categorías
Más información sobre TCP/IP Communication en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!