Effacer les filtres
Effacer les filtres

Pass text file as argument to function?

3 vues (au cours des 30 derniers jours)
Janell Lopez
Janell Lopez le 25 Sep 2016
Commenté : Janell Lopez le 26 Sep 2016
Hi,
I have a homework assignment where we have a main function that calls 3 other functions: one reads a system of equations from a text file, one solves the system, and one writes the system and its solutions to a new text file. I'm supposed to pass the name of the text file as the argument to the main function (or have the user be able to do so) so that I don't have to modify the code at all to be able to read from different text files. Right now I have a script instead of a function for the main function, and everything works if you modify the name of the text file in the code. How do I change it so that I can call the function with the text file as the argument? Do you pass it as a string? How do I call the subfunctions within the main function with a text file name as an argument?
Thank you!

Réponse acceptée

Image Analyst
Image Analyst le 25 Sep 2016
Let's say your main function is called main.m and it's a script right now. Simply put this line at the first line of that main.m file:
function main(fullFileName)
then inside the function call fgetl() or whatever you want. But then to pass the string into main, you're going to have to call uigetfile() either from the command line, or from another script or function.
  1 commentaire
Janell Lopez
Janell Lopez le 26 Sep 2016
Thanks that's what I needed!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by