You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how to generate a sine wave with my data in excel sheet?
22 views (last 30 days)
Show older comments
the values in the excel sheet are voltage and phase values with voltage being from column A to N and phase values are from column O to AB. I want to plot a sin wave with x axis as voltage and y axis as phase and also a sine wave with x axis as time and y axis as voltage. How can i do this?
Accepted Answer
dpb
on 7 Apr 2017
Edited: dpb
on 7 Apr 2017
Why did you not put the data on either two rows or in two columns; seems far more logical structure.
Still, no problem; just read the file with xlsread and reshape to get the x,y values (I didn't compute, I presume that is an even number of points each in the total; if not you'll have to adjust where the break is and even-up the two series)...
data=xlsread('yourExcelFile');
data=reshape(data,length(data)/2,[]); % make two columns from one vector: see caveats above
plot(data(:,1),data(:,2)) % phase vs amplitude
Now you need to know sampling time to compute the time vector against which to plot....
Fsamp=YourSamplingFrequency;
t=[0:length(data)-1]/Fsamp; % time vector
figure
plot(t,data(:,1)) % amplitude vs time
Label and prettify as desired...
16 Comments
Nana Fernandes
on 7 Apr 2017
i cant put those values in two rows or column as they are representing different values,
data=xlsread('yourExcelFile');
data=reshape(data(length(data)/2,[]); % make two columns from one vector: see caveats above
plot(data(:,1),data(:,2)) % phase vs amplitude
in this code will i mention my excel file as data = xlsread('excel file',sheet1,'A1:AB1') ? i want it to read values of row O1:AB1 for phase and row A1:N1 values for amplitude how can i do that with the code you gave me ? Thanks in advance
dpb
on 7 Apr 2017
"_i cant put those values in two rows or column as they are representing different values"_
??? That to me seems the perfect reason TO put them in separate row or column. I don't understand the issue you see???
You can read separate areas but not in a single call to xlsread and it is quite slow so it's far more efficient to just read the whole thing in one go.
You can create internal variables if wish, but again in Matlab using array syntax is to use the power of Matlab most; there's really no need to create additional variables. If you choose to do so, then do it inside Matlab, not in adding overhead on the read;
ampl=data(:,1);
phas=data(:,2);
clear data
Nana Fernandes
on 7 Apr 2017
Edited: Nana Fernandes
on 7 Apr 2017
Error using reshape Not enough input arguments.
Im getting this error
data=xlsread('D:\ieee14\trainingset.xlsx',1,'A1:AB6');
data=reshape(data(length(data)/2,[])); % make two columns from one vector: see caveats above
plot(data(:,1),data(:,2)) % phase vs amplitude
what am i doing wrong?
dpb
on 7 Apr 2017
Edited: dpb
on 7 Apr 2017
data=reshape(data(length(data)/2,[]));
has misplaced parens...
data=reshape(data,length(data)/2,[]);
It looks like I made a typo initially in hitting another open parens where needed a comma and didn't notice it, sorry. When you (rightfully enough) copied my error, it appears the auto-correction in Matlab suggested "fixing" the problem by adding yet another closing parenthesis to balance them out, not looking at the syntax of the function reshape to see it was missing an argument. That, of course, would be expecting a lot, but illustrates the auto-correction often doesn't even though it can help on occasion; you really need to check on what it's suggested as sometimes what it suggests will work but not produce the answer expected. Here at least it failed instead of a silent wrong answer. "There is no free lunch!"
I'll edit above as well...
Oh...
data=xlsread('D:\ieee14\trainingset.xlsx',1,'A1:AB6');
This doesn't correspond with your previous posting -- this shows a rectangular region of 6 rows? The initial description was all data in a single row but two variables depending upon the column. That's why I split in two via reshape. That logic won't work now but now we don't know what data is where???
dpb
on 7 Apr 2017
Oh...mayhaps there are now six repetitions of these results, not just one? In that case, have to split up differently...but let us know if that is really the case before we proceed to solve the wrong problem with the right answer! :)
Nana Fernandes
on 8 Apr 2017
This doesn't correspond with your previous posting -- this shows a rectangular region of 6 rows? The initial description was all data in a single row but two variables depending upon the column. That's why I split in two via reshape. That logic won't work now but now we don't know what data is where???
Im new to Matlab so i dont know much about it i just took six rows as i dont know how to input the amplitude and phase twice in the code you mentioned above
dpb
on 8 Apr 2017
Edited: dpb
on 8 Apr 2017
IF the data were as you first described, then the code I provided earlier should work just fine once you correct the typo in the argument list to reshape as I showed. Have you tried that with the correction? If so, if were an error, show us what it was...or how it didn't do what you expect.
If not that data format after all, just execute
data=xlsread('D:\ieee14\trainingset.xlsx');
whos data
and then post back the result. That will read all numeric data in the first sheet as it is and return an array containing it. The whos command will show us what that variable type and shape is to confirm what is in the file.
I have an old version of Excel that isn't .xml-enabled so I can't easily see the file and don't have time to mess with trying to convert at the moment, sorry.
ADDENDUM
I tried three online conversion facilities, the first said "Invalid file format" and failed, the second gave no error but there was nothing in the file when tried opening it in Excel and the third gave an error in Excel of "Invalid XML file" when trying to open the converted file. So, I'm at an impasse...how about saving as an old-style .XLS or .XLSX file or .CSV and attach that...can't be all that big.
Nana Fernandes
on 8 Apr 2017
Edited: Nana Fernandes
on 8 Apr 2017
Name Size Bytes Class Attributes
data 6x28 1344 double
this is the result
my file is saved as .xlsx and i have put it in a zip file and uploadeed here as the size was exceeding . i have attached a screenshot of the excel sheet so you can understand better :)
dpb
on 8 Apr 2017
Edited: dpb
on 8 Apr 2017
I unzipped your file to get the .xlsx file inside--that was the one had the aforementioned "issues".
OK, that says there are 6 rows of data but your figure only shows one. What is the definition of "who's who in the zoo?" for what is voltage, what is phase and what is the content of the other five lines.
After that, what do you want to do with it regarding all the data or just the one line or what?
On the assumption there are six separate waveforms of interest, then a minor tweaking of the previous is in order...
data=xlsread(....
data=reshape(data.',size(data,2)/2,[]); % get by length of half
v=data(:,1:2:end); % the voltage traces
p=data(:,2:2:end); % and the phase
You could, of course, also directly address the two subsets of LH/RH initially
data=xlsread(....
N=size(data,2)/2;
v=data(:,1:N).'; % LH part is voltage
p=data(:,N+1:end).'; % and the phase is RH
Transpose to column vectors as plot and other Matlab functions work on column-oriented data by default.
Nana Fernandes
on 9 Apr 2017
there is only one row in which the column A1-N1 represent voltage and column O1-AB1 represent phase
dpb
on 9 Apr 2017
Edited: dpb
on 9 Apr 2017
Well, that's what happens when assuming...right solution, wrong problem! :)
OK then
- If don't have any use for any of the other data in the spread sheet, then just read the one (first) row
data=xlsread('D:\ieee14\trainingset.xlsx',1,'1:1'); % row 1 only
Then the original code will work just fine having fixed the typo
2. If you have reason to need the other rows of data as well for some other analysis, then revert to the above to return the larger array but modify the subsequent conversion to only use the first row and you can eliminate it from the rest to have only the other data separate (presuming they're similar; if again there's a difference in content by row, just address the row in question specifically when desired..
data=xlsread('D:\ieee14\trainingset.xlsx'); % read all
vp=reshape(data(1,:),size(data,2)/2,[]); % two columns, v, p
Here's just a simple illustration of what the above does...
>> data=1:12; % simulate your row of data so can see values/location
>> vp=reshape(data,length(data)/2,[]) % two columns of half length
vp =
1 7
2 8
3 9
4 10
5 11
6 12
>>
You see the first half are in the first column, the second in the second. That is just a rearrangement of the referenced memory storage order of consecutive elements by column,row instead of just a single index by row.
dpb
on 9 Apr 2017
"...file is saved as .xlsx and i have put it in a zip file"
I just double-checked to ensure I had tried the .xlsx conversion directly; indeed it can't recognize it as a proprietary Excel .xlsx file. Looking internally, there are references to workbook/sheet1.xml albeit it is still compressed/encrypted some way not, just ascii .xml. Not sure where the hangup is but the online conversion utilities I tried couldn't do anything with it, either.
Not that it matters now that we've resolved what part of it you're actually after for this exercise, but you might try just for sake of completeness to Save it under a 'Save As' option to allow for being read by older versions of Excel. Not sure what are listed options in new version, but bound to be some. I've been able to read most all previous .xlsx spreadsheets with the MS conversion addon but yours seems a revision too far for any tools I've got...
Nana Fernandes
on 9 Apr 2017
Edited: Nana Fernandes
on 9 Apr 2017
I have attached the excel file with .xls extension just in case , hope this one opens
Your code is working just fine :) Thanks alot for that
i wanted a sine wave but i wasnt getting one so i made few changes in the code
data=xlsread('D:\ieee14\trainingset1.xls');
N=size(data,2)/2;
v=data(:,1:N).'; % LH part is voltage
p=data(:,N+1:end).'; % and the phase is RH
f = 60;
w = 2*pi*f;
fs = 2000;
T = 0:1/fs:1;
Sig = v*cos((w*T) + p);
plot(Sig)
this code gives me the following error
Error using + Matrix dimensions must agree.
Error in trailsine (line 19) Sig = v*cos((w*T) + p);
what am i doing wrong?
Ps my previous code for plotting was as follows ; i was getting a wave but it wasnt sine
data=xlsread('D:\ieee14\trainingset1.xls');
N=size(data,2)/2;
v=data(:,1:N).'; % LH part is voltage
p=data(:,N+1:end).'; % and the phase is RH
plot(v,p)
dpb
on 9 Apr 2017
Edited: dpb
on 9 Apr 2017
Well, in
Sig = v*cos((w*T) + p);
you're trying to multiply a vector v that was 14 elements long times another that is going to be 2001 elements--that doesn't work no matter which multiplication operator you choose; and here you've used * which is the matrix algebra definition. .* or "dot" multiplication would be element-by-element product, but that can't work, either, because of the size mismatch.
I'm not sure what you're trying to do here with the other data in conjunction with the generation of a new waveform, but that's the specific error Matlab is complaining about.
As for the plot, you said you wanted the phase vs voltage from the data in the spreadsheet; that's what that particular trace is. I wouldn't expect it to look like a sine wave at all necessarily; there's nothing that says what some phase angle would be at a given amplitude excepting whatever process it was that generated the values; if they were sampled from some physical system as I kinda' presumed initially, then they would reflect the transfer function of that system. I was kinda' also assuming initially given that request it was probably data from a frequency response curve rather than time trace. In fact, with the data as given, what you get is a pretty funky-looking trace that doesn't make a lot of sense at all... :)
Sometimes while you can plot any variable against another and Matlab will just do what it's asked to do it may not make much sense to use two particular variables.
Oh, that file is readily read by Excel here, btw...
Nana Fernandes
on 10 Apr 2017
Edited: Nana Fernandes
on 10 Apr 2017
i know this might seem like a dumb question but how can i make the other elements 14 element long? Should i input more values for frequency and w ? if yes then how so? i feel i might have to give it like a range eg
f= 60:100;
is this right?
btw the values in the excel sheet are voltage and phase values only
dpb
on 10 Apr 2017
"t how can i make the other elements 14 element long?"
Well, to have a given number of elements, linspace is handy as then you hand off the spacing calculation to it...
f=linspace(60,100,14);
But, to generate a smooth sine (or other) waveform you have to have sufficient sampling rate to have enough samples within the highest frequency content in the signal. This means to have only 14 samples you need to ensure those samples cover only the time span of one or two cycles at most and even that is going to be pretty rough.
What's magic about using only 14 samples? Modern PCs have essentially unlimited memory; why not use some of it to model your waveform in some detail?
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)