MATLAB Answers

How to select every second number from a file of numbers separated by a comma

조회 수: 11(최근 30일)
Alex Dimko
Alex Dimko 25 Oct 2020
댓글: Star Strider 25 Oct 2020
Hello
we were given an assignment in MATLAB at our university.
I have no programming experience with MATLAB whatsoever and now we are required to complete this seemingly daunting assignment.
Could anyone please explain to me where should I even start in completing this assignment. MATLAB code is essentially like reading chinese.
I am currently stuck at writting the code for selecting every second number from a text file full of numbers separated by a comma. The tutor at UNI advises to use a for loop.
I linked the pdf of the assignment and the text document with the numbers. We are supposed to select every second number from a row of 4 numbers. For example: 1 2 3 4 1 2 3 4 1 2 3 4... and every number marked by 2 is the number we have to select.
I have esentially no idea what to do.
Thanks in advance

  댓글 수: 0

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 25 Oct 2020
I looked at ‘UE1.txt’ and I initially could not make any sense out of it. It’s apparently supposed to be a multi-channel recording, however it imports as a (1x57604) vector.
Guessing as to its structure, this will parse it into what appear to be the appropriate vectors:
D = load('UE1.txt'); % Data Vector
Dm = reshape(D, 4, []); % ‘D’ Matrix
figure
for k = 1:3
subplot(3,1,k)
plot(Dm(1,:), Dm(k+1,:))
title(sprintf('Channel %d',k+1))
grid
xlim([0 10])
end
I normally do not provide excessive amounts of assistance for homework assignments, however if you have not been introduced to the requisite functions (such as reshape) for this, some assistance is necessary. The first row of the signal matrix ‘Dm’ after using the reshape call appears to be the time base, and the others are signals. You can figure out the sampling frequency easily enough from the time vector ‘DM(1,:)’.
I have plotted the signals as appear to be appropriate. I leave the rest to you!

  댓글 수: 5

표시 이전 댓글 수: 2
Star Strider
Star Strider 25 Oct 2020
My pleasure!
I woud begin with what David Hill advised, as well as Getting Started, and look up the functions, starting with reshape (that I linked to here, so click on the link to bring up the associated documentation). MATLAB has become a huge programming environment (when I first began to use it in the early 1990s all the documentation was in a few hardopy manuals), so search the online documentation for the information you need to use the core functions (such as reshape) and necessary Toolbox functions, if you have them and need their functions.
Alex Dimko
Alex Dimko 25 Oct 2020
Thanks. You already helped me way more than my lecture.

댓글을 달려면 로그인하십시오.

추가 답변(1개)

David Hill
David Hill 25 Oct 2020
a=reshape(load('UE1.txt'),4,[])';
yourNumbers=a(:,2);

  댓글 수: 0

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by