Read notepad into a variable

Hi,
Id like to read a notepad file row and make that into a variable
txt{6}(1:end-1)=File(cals,txt{6}(1:end-1)); (says Conversion to char from struct is not possible)
I can run this command: txt{6}(1:end-1) and it would give me, ans="x"
i can also run A=File(cals,txt{6}(1:end-1)); and it would output a 1 by 1 structure with a value inside it.
Any suggetions?
Do i need to use eval? etc
Thank you!

7 件のコメント

rockstar49
rockstar49 2023 年 5 月 25 日
That is fine, is there a way to show this in the code?
Steven Lord
Steven Lord 2023 年 5 月 25 日
Can you attach a small sample file containing data representative of the data that you want to read into MATLAB and describe specifically how you want that data to be organized in MATLAB?
If the data was stored in a way that the file contains valid MATLAB code, the solution may be as straightforward as renaming the file from a .txt file to a .m file and running it.
Walter Roberson
Walter Roberson 2023 年 5 月 25 日
I suspect that rockstar used importdata() on a file that had a mix of text and numbers.
rockstar49
rockstar49 2023 年 5 月 25 日
編集済み: rockstar49 2023 年 5 月 25 日
I can run this command: txt{6}(1:end-1) and it would give me, ans="x"
x would be a name like "animal"
in this example it shows
A=struct with fields:
Animals: 500
File(cals,txt{6}(1:end-1)); is only looking into numbers
Does this somewhat help?
Steven Lord
Steven Lord 2023 年 5 月 25 日
How did you create the variable named txt?
rockstar49
rockstar49 2023 年 5 月 26 日
I used importdata (notepad file)

サインインしてコメントする。

回答 (2 件)

Diwakar Diwakar
Diwakar Diwakar 2023 年 5 月 25 日

0 投票

% Specify the path to the text file
filePath = 'path/to/your/file.txt';
% Read the text file
fileData = fileread(filePath);
% Split the file contents by newline character to get individual rows
rows = split(fileData, '\n');
% Select the desired row (in this example, row number 6)
rowIndex = 6;
selectedRow = rows{rowIndex};
% Display the selected row
disp(selectedRow);
% Store the selected row in a variable
myVariable = selectedRow;
You are trying to assign its value to the variable txt{6}(1:end-1) directly, which is causing the error. Try this code.

8 件のコメント

rockstar49
rockstar49 2023 年 5 月 25 日
Are you able to loop this? There are multiple rows in the notepad that i want to run through and assign it to the output of =File(cals,txt{6}(1:end-1));
for ex:
Animals=File(cals,txt{6}(1:end-1));
txt(6)= 1 by 1 cell array {' Animals;'}
dogs=File(cals,txt{7}(1:end-1));
Cats=File(cals,txt{8}(1:end-1));
txt(6)= 1 by 1 cell array {' Animals;'}
Walter Roberson
Walter Roberson 2023 年 5 月 25 日
Please show your code that creates File -- or if it is a function, please show us the code for the function.
rockstar49
rockstar49 2023 年 5 月 26 日
The function files gets numeric values from a csv and writes it into the workspace
Walter Roberson
Walter Roberson 2023 年 5 月 26 日
The function File is returning a struct. We do not know what the fields of the struct look like.
I am also unclear as to what the desired outcome is.
I used importdata (notepad file)
Personally I avoid using importdata as the datatype it returns can depend upon the content of the file, so you have to check the class() of what it returns to figure out what kind of data the file happened to have.
Diwakar Diwakar
Diwakar Diwakar 2023 年 5 月 27 日
could you please show me sample of notepad file ?
rockstar49
rockstar49 2023 年 5 月 27 日
Notepad is formatted like this etc
dogs;
cats;
horses;
cows;
Stephen23
Stephen23 2023 年 5 月 27 日
Just use READCELLS or READLINES or similar.
Walter Roberson
Walter Roberson 2023 年 5 月 27 日
An example extract from the file would help.
As would a clearer description of what you are trying to do with the information.

サインインしてコメントする。

Image Analyst
Image Analyst 2023 年 5 月 28 日

0 投票

Try
textLines = readlines(fileName);
If you have any more questions, then attach your text file with the paperclip icon after you read this:

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

製品

リリース

R2021a

質問済み:

2023 年 5 月 25 日

回答済み:

2023 年 5 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by