Error using save, too many output arguments - don’t know how to fix.

Hi y’all

I am trying to save multiple tables I’ve edited in matlab, but I get this error:

 Error using save 
 Too many output arguments.

I’m having trouble figuring out how to fix this. The code it’s probably referring to is:

 function [output] = save_on_Computer(ReferenceData, month, day) 
    file_name     = "OL_"+month+"_"+day+".csv" ; 
    savefile      = save(file_name) ; 
    output        = savefile ; 
 end

from the full code here:

 % Pull data from files 
 function [output]   = each_day_table(month, day)
   fileNames        = sprintf('D2024%02d%02d*.csv', month, day) ;
   datastore_result = datastore(fileNames) ;
   original_data    = readall(datastore_result) ;
   output           = original_data ;
 end
 % Find Where Equivilant Diamiter > 150 And Remove (ICB measures b/w 2 - 150 μm)
 function [output] = remove_150(original)
    new = original ;
    Greater150 = original.EquivDiameter > 150 ;
    new(Greater150, :) = [] ; 
    output = new ;
 end
 % To Save Each Newly Made File Onto Computer 
 function [output] = save_on_Computer(ReferenceData, month, day) 
    file_name = "OL_"+month+"_"+day+".csv" ; 
    savefile = save(file_name) ; 
    output = savefile ; 
 end
 function [output] = do_everything(year, month, day) 
    original_data = each_day_table(month, day) ; 
    data_lessthan_150 = remove_150( original_data) ;
    save_to_folder = save_on_Computer(data_without_zeroes, month, day) ; 
    output = save_on_Computer ; % change data_with_surface_area with data_with_ratio
 end
 OL_06_03 = do_everything (2024,06,03) ;
 OL_06_04 = do_everything (2024,06,04) ;
 OL_06_05 = do_everything (2024,06,05) ;
 OL_06_06 = do_everything (2024,06,06) ;

 Respuesta aceptada

dpb
dpb el 23 de Mzo. de 2025
Editada: dpb el 24 de Mzo. de 2025
Indeed, the problem is in
function [output] = save_on_Computer(ReferenceData, month, day)
file_name = "OL_"+month+"_"+day+".csv" ;
savefile = save(file_name) ;
output = savefile ;
end
The <save> function doesn't return any output; it just writes the requested data to file...if you want the returned save'ed filename, then just
function file_name = save_on_Computer(ReferenceData, month, day)
% saves present
file_name = "OL_"+month+"_"+day+".csv";
save(file_name)
end
is all you need/want, although I'd note the above doesn't provide any opportunity to set an output target folder other than whatever is the <pwd> at the time the function is called.
It also will not write a csv file despite the name; instead it will save all of the local variables in scope (|ReferenceData|, month, day, and file_name) as variables in a .mat file with the given name.
One presumes your real intent is to write only the ReferenceData and those as a .csv file; that actually cannot be done by save; it can write a tab-delimited text file, but not comma-separated, and it also requires that the data be a vector or 2D array at most (no structs, tables, etc.).
If that's good enough, then
function file_name = save_on_Computer(ReferenceData, month, day)
% saves present
file_name = "OL_"+month+"_"+day+".txt";
save(file_name,'ReferenceData','-ascii','-double','-tabs') % 16 digits, '-single' --> 8 digits
end
To write a .csv file, use writematrix --
function file_name = save_on_Computer(ReferenceData, month, day)
% saves present
file_name = "OL_"+month+"_"+day+".csv";
writematrix(ReferenceData,file_name) % writematrix will use file extension and write comma-delimited
end
Again, you're limited to arrays and if pass an array of more than 2D, then the trailing dimensions are collapsed.

10 comentarios

Oh, good point about it writing a .mat file if you do not use the '-ascii' or '-ascii','-double' options !
I assumed the save function works the way we use save in other software, but that was inccorect. The write functions is what I meant to use.
I'm also not understanding how we define an output. Removing the word output in the code makes the code run, and the files I wanted are saved to my computer. I interpret a file being saved as an output, but MATLAB interprets this as a "side effect". Can anyone explain how an output vs. side effects are defined. I havn't found an easy to understand resource.
dpb
dpb el 24 de Mzo. de 2025
Editada: dpb el 24 de Mzo. de 2025
An "output" in this MATLAB context is one or more returned variables -- see the documentation the definition of <function> syntax for how "input" and "output" are used.
The code above didn't change by "remove the word output" in the sense that the specific word had any meaning, if you read the documentation for the MATLAB save function, you'll observe it has no "output" so having anything on the LHS (left hand side) of the "=" assignment operator is a syntax error and will fail--in that context what is on the LHS is a variable given that name--it would make no difference what the variable name chosen would be; it is a syntax error to try to assign a return value from a function that is defined to have no return values.
If you mean to compare the "Save" user menu choice of an application; that is simply a way for the given application to invoke internally code similar to yours; just that you aren't writing the code in Excel or similar applications as you do in MATLAB. Both end up at the same end as side effects of the actual code; while some output functions such as fprintf do have a return value (in its case it will, if asked, return how many bytes were written), Mathworks chose to not have save or the family of writeXXX (where "XXX" --> "cell", "matrix", "table", etc., ...) return anything.
"Can anyone explain how an output vs. side effects are defined. I havn't found an easy to understand resource."
How to call functions with outputs is explained here:
in particular
The term "side effect" is not generally used in MATLAB.
I assumed the save function works the way we use save in other software,
  • Ada does not have any equivalent to save()
  • C does not have any equivalent to save()
  • C++ does not have any equivalent to save()
  • C# does not have any equivalent to save()
  • Common Lisp does not have any equivalent to save()
  • Fortran does not have any equivalent to save()
  • go does not have any equivalent to save()
  • java does not have any equivalent to save()
  • javascript does not have any equivalent to save()
  • Lua does not offer any equivalent to save()
  • maple offers save() but save() does not return any value
  • Mathematica offers Save(), but Save() does not return any value
  • python uses pickle. pickle.dump() writes to file. pickle.dumps() returns a byte stream (not a file name)
  • R uses save(), but R's save() does not offer the possibility of returning anything
I got tired after that point.
I have to wonder which other software packages offer a save-equivalent that returns a file name.
  • Excel uses Save
  • Word uses Save
  • Acrobat uses Save
  • MATLAB Editor uses Save
I tire much more easily... :)
@Kristine is clearly new to MATLAB and any programming environment wherein such operations are not simply menu options but require actually writing the specific code. The "Save/Save As" menu item is ubiquitous in packaged applications and if that is all one has ever used before, it's not surprising to have that as a working model for comparison.
I cannot think of any menu Save commands that return the file name.
dpb
dpb el 25 de Mzo. de 2025
Editada: dpb el 25 de Mzo. de 2025
I think that is irrelevant and only indicates that @Kristine didn't understand MATLAB syntax well enough to understand what the documentation meant with regards to "inputs" and "outputs".
The standard file selection menu one gets has the place to select and open the output file or create a name for a new file; I can certainly see how a general non-programmer user could interpret that as "returning" the file name...it certainly is subsequently passed internally to the code that does the actual file manipulation so, in fact, it is returned; just not to the user, but certainly to the calling function.
This just seemed like picking on a newbie to me, Walter...
I can certainly see how a general non-programmer user could interpret that as "returning" the file name...
I personally do not see how anyone reasonable could interpret menu saving commands as "returning" file names in a way that could be assigned to variables.
Oh, so little imagination... :)

Iniciar sesión para comentar.

Más respuestas (2)

Steven Lord
Steven Lord el 23 de Mzo. de 2025
None of the syntaxes listed in the Syntax section of the documentation page of the save function indicate it returns any output. If you tell us what you were hoping that variable would contain we may be able to tell you how to create that variable.
save() does not support any output arguments.
You also have the problem that save() with a single input argument will save all of the variables in scope -- including ReferenceData, month, day and file_name
function [output] = save_on_Computer(ReferenceData, month, day)
file_name = "OL_"+month+"_"+day+".csv" ;
save(file_name, 'ReferenceData');
output = file_name ;
end

Categorías

Productos

Versión

R2024b

Etiquetas

Preguntada:

el 23 de Mzo. de 2025

Comentada:

dpb
el 26 de Mzo. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by