Dear all;
I have a ready code contain this command within a function
---------------------------------------------------
[pvdata,~]=xlsread([Newfolder, Mono70W999]);
when i lunch it an error appear ( tha path and the file names are correct)
Unrecognized function or variable 'Newfolder'.
Error in PVModuleCurveFitting/Open (line 85)
[pvdata,~]=xlsread([Newfolder, Mono70W999])
Error while evaluating UIControl Callback.
  • I uhave problem understaning the use of square brackets in xlsread because i used to work with these syntax
num = xlsread(filename)
num = xlsread(filename,sheet)
num = xlsread(filename,xlRange)
num = xlsread(filename,sheet,xlRange)
num = xlsread(filename,sheet,xlRange,'basic')
[num,txt,raw] = xlsread(___)
  • also i want to know the purpose of using ~ alone ?

 Réponse acceptée

Voss
Voss le 26 Fév 2024
Modifié(e) : Voss le 26 Fév 2024
1. The error, "Unrecognized function or variable 'Newfolder'." means that the variable 'Newfolder' is not defined in the function that's calling xlsread, at the time xlsread is called.
2. The square brackets construct a file name by horizontally concatenating the variables 'Newfolder' and 'Mono70W999'. Example:
Newfolder = 'C:\Data';
Mono70W999 = 'Project1.xlsx';
result = [Newfolder, Mono70W999]
result = 'C:\DataProject1.xlsx'
Then that file name is passed to xlsread as the only input, so this is using the "num = xlsread(filename)" syntax.
Note that fullfile is better than square brackets for constructing file paths because it inserts directory separators between its arguments as needed. Example:
result = fullfile(Newfolder, Mono70W999)
result = 'C:\Data/Project1.xlsx'
[MATLAB Answers runs on a Linux platform,which uses slash ( / ) so that's why that's there. If you run it in Windows you'd get a backslash ( \ ) instead.]
3. The purpose of ~ is to indicate that the second output from xlsread is discarded. In this case, the ~ is unnecessary because the first output of xlsread is the same regardless of how many outputs are requested, so you'd get the same behavior with out = xlsread(filename) syntax.
Some other function besides xlsread might output different things depending on how many outputs are requested, in which case the [out1,~] = func(in) syntax might be necessary.

14 commentaires

Thank you fo your answer
i tried literally what you said
still facing a problem with different error due to my misunderstanding of () and []
here is my modification
function Open(~,~)
Newfolder= 'C:\Users\HA\Desktop\PV Parameter estimation\CurveFittingTools\Newfolder';
Mono70W999 = 'Project1.xlsx';
[filename, pathname] = uigetfile('*.csv');
if isequal(filename,0) % Handling Cancel button pressed
return;
end
pvdata = xlsread[Newfolder, Project1];
set(hPmax,'String',num2str(pvdata(6,2)));
And here is the error
Error: File: PVModuleCurveFitting.m Line: 87 Column: 25
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.
Voss
Voss le 27 Fév 2024
Presuming that you want to read the file that was selected:
function Open(~,~)
[filename, pathname] = uigetfile('*.csv');
if isequal(filename,0) % Handling Cancel button pressed
return;
end
pvdata = xlsread(fullfile(pathname,filename));
Although readmatrix or readtable is recommended over xlsread.
YASSER
YASSER le 27 Fév 2024
Déplacé(e) : Stephen23 le 27 Fév 2024
Dear sir; as you see in attached photo
I always get an ampty [] from pvdata which mean that am not loading the csv file as i am encircling in workspace and command windows.
Stephen23
Stephen23 le 27 Fév 2024
Please upload the CSV file Mono70W999.csv by clicking the paperclip button.
As Voss already recommended, you should use READTABLE or READMATRIX instead of XLSREAD.
Note that CSV files are not Excel files, they are text files. XLSREAD is the wrong tool.
Voss
Voss le 27 Fév 2024
@YASSER: The file you have circled, Mono70W999.csv, appears to be located in 'C:\Users\HA\Desktop\PV Parameter estimation\CurveFittingTools', which makes its full path as follows:
'C:\Users\HA\Desktop\PV Parameter estimation\CurveFittingTools\Mono70W999.csv'
but you are trying to read
'C:\Users\HA\Desktop\PV Parameter estimation\CurveFittingTools\Newfolder\Mono70W999.csv'
Do you see the difference?
Also, why are you bothering to call uigetfile if you're not going to use its output?
I wrote:
pvdata = xlsread(fullfile(pathname,filename));
And you are doing:
pvdata = xlsread(fullfile(Newfolder,Mono70W999));
Do you want to read whatever file the user selected via uigetfile? If that's the case, use what I wrote and remove the variables Newfolder and Mono70W999.
Or do you want to always read the same Mono70W999.csv file, regardless of what file was selected with uigetfile? If that's the case, use what you wrote and remove the call to uigetfile.
YASSER
YASSER le 27 Fév 2024
1-The file is located both paths: the one that you see and the second is in the new folder , so this is not an issue
2-i am calling uigetfile because am going to change parameter to estimate others
So am gonna use it's output later
3- Do you want to read whatever file the user selected via uigetfile? my answer is yes, and yes i used both (what u wrote and also my variables) alwyas got emty []
My only issue is that i want to load this csv data by using only xlsread, and not readtable or others
Voss
Voss le 27 Fév 2024
Please do as Stephen suggested and upload the file using the paperclip button.
YASSER
YASSER le 27 Fév 2024
i have to upload using Xlsread Because there a bunch of steps to foillow depending on this step
Concern the type csv ,I can change it to xlsx and i know the disavantages of doing it
But i have to read using Xlsread
And i always get an empty []
Voss
Voss le 27 Fév 2024
Modifié(e) : Voss le 27 Fév 2024
Please upload the file to this forum by clicking the paperclip button in the Insert section of the toolbar above where you type a comment.
(Paperclip button to upload a file to the Answers forum, circled in green.)
Steven Lord
Steven Lord le 27 Fév 2024
Without seeing the actual file I think it's going to be impossible or nearly impossible to offer any suggestions for how to resolve the problem where you receive an [] as the output from xlsread. Just based on what we know already, I'm guessing the CSV file is empty. Seeing the actual file is the only way to confirm or refute my guess. So please attach the file to a comment as Stephen23 and Voss have requested using the button above the editor where you type your comment.
YASSER
YASSER le 28 Fév 2024
Sorry to be late
The file is not empty
I used it as CSV and xlsx without result
I have upload both csv and code
I appretiate your Help
Voss
Voss le 28 Fév 2024
Modifié(e) : Voss le 28 Fév 2024
pathname = '.';
filename = 'Mono70W9999.csv';
readtable can read the file properly:
readtable(fullfile(pathname,filename),'NumHeaderLines',15,'VariableNamingRule','preserve','VariableNamesLine',15)
ans = 151x3 table
V (V) I (A) P (W) _____ _____ ______ 0 5 0 0.13 4.999 0.6757 0.27 4.997 1.351 0.4 4.996 2.0259 0.54 4.994 2.7004 0.67 4.993 3.3746 0.81 4.992 4.0483 0.94 4.99 4.7217 1.08 4.989 5.3947 1.21 4.987 6.0672 1.35 4.986 6.7394 1.48 4.984 7.4112 1.62 4.983 8.0826 1.75 4.981 8.7536 1.89 4.98 9.4243 2.02 4.979 10.095
However, from your code I gather that you need the parameter values in the header as well, in which case it is convenient to use readmatrix instead:
pvdata = readmatrix(fullfile(pathname,filename))
pvdata = 166x3
NaN 9.0000 NaN NaN NaN NaN NaN NaN NaN NaN 20.2700 NaN NaN 5.0000 NaN NaN 69.5253 NaN NaN 15.4800 NaN NaN 4.4910 NaN NaN 3.5760 NaN NaN 0.6850 NaN
disp(pvdata(4:14,2)) % parameters
20.2700 5.0000 69.5253 15.4800 4.4910 3.5760 0.6850 41.9000 999.0000 36.0000 0.6378
disp(pvdata(16:end,:)) % data
0 5.0000 0 0.1300 4.9990 0.6757 0.2700 4.9970 1.3510 0.4000 4.9960 2.0259 0.5400 4.9940 2.7004 0.6700 4.9930 3.3746 0.8100 4.9920 4.0483 0.9400 4.9900 4.7217 1.0800 4.9890 5.3947 1.2100 4.9870 6.0672 1.3500 4.9860 6.7394 1.4800 4.9840 7.4112 1.6200 4.9830 8.0826 1.7500 4.9810 8.7536 1.8900 4.9800 9.4243 2.0200 4.9790 10.0945 2.1600 4.9770 10.7643 2.2900 4.9760 11.4338 2.4300 4.9740 12.1028 2.5600 4.9730 12.7715 2.7000 4.9710 13.4397 2.8300 4.9700 14.1076 2.9700 4.9680 14.7751 3.1000 4.9670 15.4422 3.2400 4.9650 16.1082 3.3700 4.9640 16.7737 3.5100 4.9620 17.4388 3.6400 4.9600 18.1010 3.7800 4.9560 18.7595 3.9100 4.9540 19.4190 4.0500 4.9520 20.0795 4.1800 4.9490 20.7395 4.3200 4.9470 21.4005 4.4600 4.9460 22.0616 4.5900 4.9440 22.7222 4.7300 4.9420 23.3825 4.8600 4.9410 24.0438 5.0000 4.9400 24.7048 5.1300 4.9380 25.3654 5.2700 4.9370 26.0256 5.4000 4.9360 26.6862 5.5400 4.9350 27.3478 5.6700 4.9340 28.0091 5.8100 4.9330 28.6702 5.9400 4.9320 29.3310 6.0800 4.9310 29.9915 6.2100 4.9300 30.6517 6.3500 4.9290 31.3126 6.4800 4.9280 31.9735 6.6200 4.9270 32.6341 6.7500 4.9260 33.2945 6.8900 4.9250 33.9547 7.0200 4.9250 34.6147 7.1600 4.9240 35.2745 7.2900 4.9230 35.9340 7.4300 4.9220 36.5930 7.5600 4.9210 37.2517 7.7000 4.9200 37.9102 7.8300 4.9190 38.5684 7.9700 4.9190 39.2263 8.1000 4.9180 39.8841 8.2400 4.9170 40.5415 8.3700 4.9160 41.1988 8.5100 4.9150 41.8552 8.6500 4.9140 42.5111 8.7800 4.9130 43.1668 8.9200 4.9120 43.8222 9.0500 4.9110 44.4774 9.1900 4.9100 45.1323 9.3200 4.9090 45.7869 9.4600 4.9080 46.4405 9.5900 4.9070 47.0933 9.7300 4.9060 47.7458 9.8600 4.9050 48.3981 10.0000 4.9040 49.0500 10.1300 4.9020 49.7016 10.2700 4.9010 50.3524 10.4000 4.9000 51.0007 10.5400 4.8990 51.6487 10.6700 4.8970 52.2963 10.8100 4.8960 52.9435 10.9400 4.8950 53.5904 11.0800 4.8930 54.2344 11.2100 4.8910 54.8775 11.3500 4.8900 55.5202 11.4800 4.8880 56.1625 11.6200 4.8860 56.8016 11.7500 4.8840 57.4314 11.8900 4.8810 58.0604 12.0200 4.8780 58.6862 12.1600 4.8750 59.3068 12.2900 4.8720 59.9264 12.4300 4.8680 60.5323 12.5600 4.8630 61.1352 12.7000 4.8580 61.7301 12.8400 4.8530 62.3186 12.9700 4.8470 62.8993 13.1100 4.8410 63.4719 13.2400 4.8330 64.0265 13.3800 4.8230 64.5487 13.5100 4.8130 65.0616 13.6500 4.8010 65.5490 13.7800 4.7890 66.0307 13.9200 4.7780 66.5169 14.0500 4.7620 66.9433 14.1900 4.7480 67.3856 14.3200 4.7280 67.7500 14.4600 4.7080 68.0906 14.5900 4.6880 68.4358 14.7300 4.6660 68.7463 14.8600 4.6400 68.9859 15.0000 4.6140 69.2234 15.1300 4.5810 69.3483 15.2700 4.5460 69.4439 15.4000 4.5110 69.5079 15.5400 4.4630 69.3841 15.6700 4.4210 69.3184 15.8100 4.3810 69.2837 15.9400 4.3280 69.0406 16.0800 4.2650 68.6034 16.2100 4.2080 68.2526 16.3500 4.1350 67.6258 16.4800 4.0560 66.8945 16.6200 4.0050 66.5871 16.7500 3.9230 65.7553 16.8900 3.8500 65.0488 17.0300 3.7540 63.9358 17.1600 3.6640 62.9049 17.3000 3.5760 61.8706 17.4300 3.4620 60.3629 17.5700 3.3010 58.0125 17.7000 3.2300 57.1998 17.8400 3.1280 55.8244 17.9700 2.9890 53.7427 18.1100 2.8360 51.3760 18.2400 2.6960 49.2024 18.3800 2.5500 46.8830 18.5100 2.3950 44.3570 18.6500 2.2390 41.7765 18.7800 2.0680 38.8652 18.9200 1.9140 36.2186 19.0500 1.7460 33.2803 19.1900 1.5720 30.1824 19.3200 1.4170 27.3951 19.4600 1.2510 24.3510 19.5900 1.0440 20.4681 19.7300 0.8750 17.2709 19.8600 0.6240 12.4091 20.0000 0.4350 8.7141 20.1300 0.2000 4.0301 20.2700 0 0
plot(pvdata(16:end,1),pvdata(16:end,2))
xlsread generates an error with this file (it does not return an empty array):
xlsread(fullfile(pathname,filename))
Error using xlsread
Unable to read XLS file /users/mss.system.pcgmv/Mono70W9999.csv. File is not in recognized format.
YASSER
YASSER le 28 Fév 2024
Thank you Mr Voss
Thank you all of you
Voss
Voss le 28 Fév 2024
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Produits

Version

R2020a

Question posée :

le 26 Fév 2024

Commenté :

le 28 Fév 2024

Community Treasure Hunt

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

Start Hunting!

Translated by