63
IV. Stringuri și Fișiere I/O (Intrare/Ieșire) TOPICS A. String controls and indicators B. String functions C. File I/O

9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

IV. Stringuri și Fișiere I/O (Intrare/Ieșire)

TOPICS

A. String controls and indicators

B. String functions

C. File I/O

Page 2: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

A. Controale și Indicatoare String

• Un rol aparte între controalele/indicatoarele din

mediul LabView îl joacă cele pentru lucrul cu șir

de caractere și tabele. Componentele din

această categorie permit citirea/afisarea acestui

tip de date.

• Un string este o secvență de caractere (ASCII)

afișabile sau neafișabile (spațiu, tab, e.t.c.)

• Utilizări – afișarea mesajelor, controlul

instrumentelor, fișiere I/O

Page 3: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Figure 4.1. Control string

Page 4: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• Normal display • Password display

• \ code display • Hex display

Moduri de afișare a stringurilor

Page 5: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• Funcţiile pentru valorile alfanumerice sunt grupate în subpaleta String a paletei de funcții. Mai frecvent utilizate sunt cele pentru transformarea caracterelor în majuscule (To Upper Case), pentru transformarea caracterelor în minuscule (To Lower Case), de calculare a lungimii unui şir de caractere (String Length), de compunere a unui şir din două şiruri distincte (Concatenate String), de identificare şi înlocuire a unui şir în cadrul altuia (Search and Replace String) etc.

• De asemenea, subpaleta String conţine un element String Constant utilizat pentru introducerea în diagramă a valorilor alfanumerice constante.

B. Funcţii String

Page 6: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Figure 4.2. Paleta cu funcții string

Page 7: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• Funcţia String Length determină numărul de caractere dintr-o valoare alfanumerică. Sunt luate în considerare şi caracterele neafişabile (spaţiu, Tab, sfârşit de linie etc). Numărul de caractere de la ieşirea funcţiei este exprimat ca un număr întreg pe patru octeţi.

• Funcţia Concatenate Strings posedă un număr de intrări ce poate fi modificat de către utilizator prin dimensionarea simbolului funcţiei (în mod asemănător cu Compound Arithmetic). La ieşirea sa, funcţia generează o valoare alfanumerică obţinută prin concatenarea textelor de la intrările sale, în ordinea de sus în jos.

Page 8: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• Funcţia String Subset extrage, din textul conectat la intrarea String, o porţiune de Length caractere începând de la caracterul aflat la poziţia Offset inclusiv (pentru primul caracter se consideră offset = 0). Dacă la intrarea Offset a funcţiei nu este introdusă nici o valoare, funcţia consideră că extragerea se va efectua începând cu primul caracter (valoarea implicită 0 a intrării offset). Dacă la intrarea Length nu este introdusă nici o valoare, funcţia consideră că extragerea se va efectua până la ultimul caracter inclusiv.

Page 9: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• Funcţia Replace Substring şterge o porţiune din textul conectat la intrarea String şi introduce în locul respectiv textul conectat la intrarea Substring. Porţiunea înlocuită începe de la caracterul aflat pe poziţia Offset inclusiv şi conţine Length caractere. Intrarea Offset are valoarea implicită 0, iar intrarea Length are o valoare implicită egală cu numărul de caractere legat la intrarea Substring. Dacă la intrarea Substring nu se leagă nici o valoare, funcţia efectuează o ştergere a unei porţiuni, iar dacă la intrarea Length se leagă valoarea 0 funcţia efectuează, de fapt, o inserare a unei porţiuni.

Page 10: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• Funcţia Search and Replace String caută textul Search String în textul conectat la intrarea String, iar când îl găseşte, îl înlocuieşte cu textul conectat la intrarea Replace String. Căutarea are loc începând de la poziţia Offset a textului String. Dacă la intrarea Offset nu se conectează nici o valoare, căutarea are loc începând cu prima poziţie. Dacă la intrarea Replace String nu se conectează nici o valoare, atunci funcţia efectuează o ştergere (înlocuire cu text vid). Dacă la intrarea Replace All nu se conectează nici o valoare, atunci funcţia se opreşte după prima apariţie a textului Search String. Dacă la această intrare se conectează o valoare logică de tip True, funcţia efectuează înlocuiri pentru toate apariţiile textului Search String. Funcţia generează textul rezultat în urma înlocuirii la ieşirea Result String, iar la ieşirea Offset Past Replacement generează o valoare indicând poziţia caracterului aflat imediat după ultimul caracter înlocuit.

Page 11: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• Funcţia Search/Split String caută textul conectat la intrarea Search String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă textul căutat este găsit, funcţia împarte textul iniţial în două părţi, astfel: partea dinaintea textului căutat (Substring Before Match) şi textul căutat urmat de restul textului în care s-a făcut căutarea (Match + Rest Of String). Funcţia generează la ieşirea Offset Of Match poziţia în care a fost găsit textul căutat. Dacă textul căutat nu a fost găsit, ieşirea Substring Before Match conţine tot textul legat la intrarea String, iar ieşirea Offset Of Match are valoarea -1.

Page 12: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• Funcția Format Into String convertește numere într-un șir de caractere (1.28→1.2800). Această funcție nu este folosită doar pentru conversia datelor numerice în date string. Mai pot fi convertite în stringuri următoarele elemente: căi de fișiere, enumerări, date Booleene. De exemplu, utilizând formatul %s un Boolean va fi convertit în "FALSE" sau "TRUE." Utilizând formatul %d, Booleanul va fi convertit în "0" sau "1." Similar, utilizând formatul %s, un Enum va fi convertit în textul său. Utilizând formatul %d, Enum va fi convertit în valoarea sa întreagă.

Page 13: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Converting Numerics to Strings

The Build String Express VI

concatenates an input string. If

the inputs are not a string, the

inputs are converted to a string

based on the configuration of the

Express VI. The Build String

Express VI is available in the

Functions»Output palette.

Page 14: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Scan From String: Converts the input string containing valid

numeric characters to individual numbers. Format string can specify

multiple outputs with differing data types (numeric, Boolean, etc.).

The function can be stretched out to add more output terminals.

Converting Strings to Numerics

Page 15: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

C. Fișiere Intrare/Ieșire (I/O)

• Operaţiile cu fişiere de intrare / ieşire (I/O) au ca scop principal stocarea datelor într-un fișier şi preluarea datelor dintr-un fişier

• Principalele operații ce pot fi realizate sunt:

Deschidere și închidere fișiere de date;

Citirea datelor din fișiere si scrierea datelor în fisiere;

Mutarea și redenumirea fișierelor si directorilor;

Crearea de directori și fișiere, specificați în controlul cale;

Scrierea de stringuri, numere, vectori și clustere în fișiere.

• Fișierele pot fi binare, text sau tabel.

• Scrierea/Citirea în fișierele de măsurare LabVIEW au extensia “lvm” (*.lvm)

Page 16: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• Operațiile tipice cu fișierele I/O presupun următoarele procese:

1. You create or open a file. After the file opens, a unique identifier called a refnum represents the file.

2. The File I/O VI or function reads from or writes to the file.

3. You close the file.

Figure 4.3. Steps in a Typical File I/O Operation

Page 17: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Formatul Fişierelor

• LabVIEW pot folosi sau crea următoarele formate de fișier: Binary, ASCII,LVM, and TDMS.

•Binary— Fișierele binare sunt formatul de fișier care stau la baza tuturor celorlalte formate de fișiere.

•ASCII—un fișier ASCII este un tip specific de fișier binar, şi care este un standard utilizat de cele mai multe programe. It consists of a series of ASCII codes. ASCII files are also called text files.

•LVM—fișierul de date de măsurare ( .lvm ) este un fișier text delimitat de tabulatori, cu ajutorul cărora se pot deschide diferite aplicații de calcul tabelar sau aplicații de editare de text. Fișierul cu extensia .lvm conţine informații despre date, cum ar fi data și ora la care datele au fost generate.

• TDMS—Acest format de fișier este un tip specific de fișier binar creat pentru produsele National Instruments.

Page 18: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Figure 4.4. Paleta cu funcții File I/O

Page 19: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Understanding High-Level File I/O • Some File I/O VIs perform all three steps of a file I/O

process—open, read/write, and close. If a VI performs all three steps, it is referred to as a high-level VI. However, these VIs may not be as efficient as the low-level VIs and functions designed for individual parts of the process. If you are writing to a file in a loop, use low-level file I/O VIs. If you are writing to a file in a single operation, you can use the high-level file I/O VIs instead.

• LabVIEW includes the following high-level file I/O VIs:

- Write to Spreadsheet File—Converts a 2D or 1D array of double-precision numbers to a text string and writes the string to a new ASCII file or appends the string to an existing file. The VI opens or creates the file before writing to it and closes it after wards. You can use this VI to create a text file readable by most spreadsheet applications.

Page 20: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

- Read From Spreadsheet File—Reads a specified number of lines or rows from a numeric text file beginning at a specified character off setand converts the data to a 2D double-precision array of numbers. The VI opens the file before reading from it and closes it afterwards. You can use this VI to read a spreadsheet file saved in text format.

- Write to Measurement File—An Express VI that writes data to a text-based measurement file (.lvm) or a binary measurement file (.tdms) format. You can specify the save method, file format (.lvm or.tdms), header type, and delimiter.

- Read from Measurement File—An Express VI that reads data from a text-based measurement file (.lvm) or a binary measurement file (.tdms) format. You can specify the file name, file format, and segment size.

Page 21: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

WRITING LABVIEW DATA TO A

SPREADSHEET FILE

• The text files created by LabVIEW can be opened in

spreadsheet programs such as Excel. Writing data to a

spreadsheet file is the mechanism used to move

LabVIEW data into a spreadsheet.

• A LabVIEW spreadsheet file is a text file that

spreadsheets such as Excel can read, but LabVIEW

does not create .xls or .xlsx files directly. However,

opening a text file in Excel is so straightforward, it

makes little difference that LabVIEW creates .txt files

instead of .xls files.

Page 22: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• The Write to Spreadsheet File function is available in the Function Palette’s Programming Group, in the File I/O group. For example, here are the directions to the Write to Spreadsheet File function:

Function Palette / Programming Group / File I/O group / Write to Spreadsheet File

• To learn how to send data from a matrix to a text file, we will create a VI that uses the Write to Spreadsheet File function.

• We begin on the front panel by creating an array and filling it with some data values.

Page 23: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Figure 4.5.

Front panel showing

data to be saved in

each text file format.

Figure 4.6.

Connection pane for

Write to Spreadsheet

File function

Page 24: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• Because we did not specify a file path, when we run the VI the Choose File to Write dialog (Figure 4.8) opens to allow the user to select a folder and enter a file name.The .txt file extension was used so that the text file will be recognized by other programs. Click OK to save the data to the “Write Test.txt” file.

• We can open the file in any program that can display a text file (Notepad was used in Figure 4.9) to see how data were saved.

Figure 4.7. Block

diagram to send Data

array values to a text

file.

Page 25: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Figure 4.8. The Choose File to Write dialog allows the user to

set the file name.

Page 26: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Figure 4.9. The data in the

Write Test.txt file.

• The values in the Data array were sent to the file, but

only three decimal places were saved. This is a result

of using the default format string (%.3f, as indicated in

Figure 4.6.) we can change the format string to save

more decimal places, if needed.

• The terminals on the Write to Spreadsheet File function

block (see Figure 4.6) include:

Page 27: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Inputs:

• format (%.3f)—the format string used to write the values to the text file.

• file path (dialog if empty)—this is a path variable indicating where the file should be stored. If no path is specified, the Choose File to Write dialog is opened to determine the path at run time.

• 2D data—the terminal used to save a 2D array or matrix of values.

• 1D data—the terminal used to save single row or column of values.

• append to file (new file: F)—a Boolean (True or False) value used to tell LabVIEW how to handle the data if the file already exists. TRUE means append the data to the data already in the file. FALSE (the default) means overwrite the existing data.

• transpose? (no: F)—a Boolean (True or False) value indicating whether or not the data should be transposed (rows and columns interchanged) before saving. The default is not to transpose the data.

• delimiter (\t)—the delimiter is the value placed between numbers to separate them. The default is a tab character (“\t”).

Page 28: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Output:

• new file path—provides programmatic access to the file path that was used to

save the data.

To see how these inputs can be used to modify the way the data are saved, we will change the format string that controls how LabVIEW writes the data to the file.

Format Strings

A format string for writing floating point numbers:

• Begins with the “%” character.

• Sometimes includes “^” (caret, [Shift 6]) to force engineering notation (scientific

notation in multiples of three, e.g., e3, e6.)

• Sometimes a “#” symbol—instructs LabVIEW to drop trailing zeros.

• Sometimes includes a period and a number (e.g., “.3”) indicating the number of

decimal places to show.

• Ends with a final letter indicating the notation style.

• f—floating point notation (decimal point)

• e—scientific notation

• g—LabVIEW uses “f” or “e” depending on size of number

Page 29: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• The following table shows how the value 12.3450000 will be saved using various formats.

%.3f (default) 12.345

%.7f 12.3450000

%#.7f 12.345

%e 1.234500e1

%.3e 1.235e1 (rounded)

%^.3e 12.345e0

%g 12.345000

%#g 12.345

• Figure 4.10 shows how the format string can be sent to the Write to Spreadsheet File function.

• The resulting file is shown in Figure 4.11. Notice that LabVIEW rounds when the “f” format is used.

Page 30: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Figure 4.10. Asking for

seven decimal places on

saved values.

Figure 4.11. Data file

created using the “%.7f”

format.

Page 31: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

WRITING LABVIEW DATA TO A

MEASUREMENT FILE • A LabVIEW measurement file (file extension .lvm) is a

text file used to save waveforms.

• A LabVIEW waveform is a collection of data values collected over time plus some additional header information. Waveforms are automatically generated when data are acquired using LabVIEW data acquisition functions.

• Measurement files are commonly used to save the data collected using a data acquisition system. LabVIEW can read measurement files to reload data for processing and analysis. And, because measurement files are text files, they can be opened by other programs such as word processors and spreadsheets.

Page 32: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• A measurement file is created using the Write to Measurement File Express VI.

• An Express VI is a function, or VI, that comes with a dialog box to help the programmer configure the required connections. You can recognize Express VIs by a blue border around the icon in the Function Palette.

• The Write to Measurement File Express VI is located in the Programming Group’s File I/O Group.

Functions Palette / Programming Group / File I/O Group / Write to Measurement File

• When the Write to Measurement File Express VI is placed on the block diagram, the icon for the function is placed on the block diagram and the Configure Write to Measurement File dialog (shown in Figure 4.12) automatically opens.

Page 33: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Figure 4.12. The Configure Write to Measurement File dialog.

Page 34: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• Six areas have been indicated on the Configure Write to Measurement File dialog (shown in Figure 4.12):

1. File Name—you can specify the name of the file to be created.

2. Action—instructs LabVIEW what to do if you run the VI multiple times

(creating multiple files).

3. File Format—the function will output text files (.lvm) and binary files.

4. Segment Headers—you can configure the data acquisition functions to take multiple data sets in a single run. Each of these would be considered a segment. The segment header provides information on when the data were collected, who collected the data, and what data were collected.

5. X Value Columns—a typical waveform contains data collected over a period of time, and time is the X value.You can select how the time values are reported in the measurement file.

6. Delimiter—the delimiter is the character that is placed between each numeric value so that the program that reads the file can tell where one number ends and the next starts.Tab delimiters are very commonly used.

Page 35: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• În figura de mai jos se exemplifică utilizarea funcţiei de scriere a datelor într-un fişier (Write To Measurement File). Se remarcă construcţia fişierului de date cu precizările referitoare la data simulării, numărul de eşantioane etc.

• LabVIEW Measurement

• Writer_Version 2

• Reader_Version 2

• Separator Tab

• Decimal_Separator ,

• Multi_Headings Yes

• X_Columns No

• Time_Pref Relative

• Operator Mihai

• Date 2009/04/27

• Time 11:01:18,363543

• ***End_of_Header***

• Channels 1

• Samples 100

• Date 2009/04/27

• Time 11:01:18,363543

• X_Dimension Time

• X0 0,0000000000000000E+0

• Delta_X 0,001000

• ***End_of_Header***

• X_Value Sine Comment

• 0,000000

• 0,063418

• 0,126580

• 0,189233

• 0,251123

• 0,312003

• 0,371627

• 0,429755

• 0,486152

• 0,540593

• 0,592857

• 0,642734

• 0,690024

• 0,734536

• 0,776090

Figure 4.13. Scrierea datelor într-un fișier

Page 36: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

READING A LABVIEW

MEASUREMENT FILE

• To read a LabVIEW measurement file, use the Read From Measurement File Express VI (see Figure 4.14) found in the File I/O group:

Functions Palette / Programming Group / File I/O Group / Read From Measurement File

• When the Read From Measurement File Express VI is placed on the block diagram, the Configure Read From Measurement File dialog (shown in Figure 4.15) automatically opens. You should ensure that the correct .lvm file is indicated in the File Name field, or check Ask user to choose file in the Action section to select the desired file at run time.

Page 37: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Figure 4.15. The Configure Read From

Measurement File dialog.

Figure 4.14. Block

diagram for a VI

to read a

measurement

file and plot the

values.

Page 38: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• Exemplificarea citirii unui fişier de măsurători test_2.lvm şi vizaualizarea informaţiei acetuia este prezentată în figura de mai jos.

Figure 4.16. Citirea datelor dintr-un fișier

Page 39: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

READING A SPREADSHEET FILE IN

LABVIEW

• LabVIEW provides a Read From Spreadsheet File function, which can be used to read values from a text file into LabVIEW.

Functions Palette / Programming Group / File I/O Group / Read From Spreadsheet File

Note: LabVIEW does not read Excel files directly. But an Excel workbook can be saved as a tab-delimited text file (.txt file extension) and LabVIEW can read it.

• The general process for getting data from an Excel workbook into LabVIEW is as follows:

Page 40: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

1. Get the data values (no text column headers) into an Excel worksheet.

a. Make sure it is the only worksheet in the workbook.

b. The top-left value should be in cell A1.

2. Save the Excel file as a .txt file.

3. Place a Read From Spreadsheet File function on a LabVIEW block diagram.

4. Select the data type of the values to be read from the file.

5. Use a string constant to specify the format string that should be used to read the values, if desired. The default format string is “%.3f”, which reads floating point numbers with three decimal places.

6. Use a string constant to specify the path name if desired. If the path name is omitted, LabVIEW will ask the user to choose the file at run time.

7. Add an indicator to the block diagram to display the values read from the file.

Page 41: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Step 1: Get the data values into an Excel worksheet

• Figure 4.17 shows an Excel file containing temperature

data from seven thermocouples collected over a period

of 1 hour. It is a typical spreadsheet with

• Title information at the top

• Column headings

• Graph

• Three worksheets in the workbook

• All of that information needs to be deleted before saving

the workbook as a text file that LabVIEW can read.

Figure 4.18 shows the same temperature and time data

in a worksheet by itself.

Page 42: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Figure 4.17. Excel file with extraneous information.

Page 43: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Figure 4.18. Excel worksheet with extraneous information removed.

Page 44: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Step 2: Save the Excel file as a .txt file

• To save the Excel worksheet as a tab-delimited text file, start with the following menu options, illustrated in Figure 4.19:

Office button / Save As / Other Formats Figure 4.19. Using the Office button to save the Excel worksheet

in another format.

Page 45: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• This opens the Save As dialog shown in Figure 4.20. Select Text (Tab delimited) (*.txt) in the Save as type: field.

Figure 4.20. Select tab-delimited text file in the Save As dialog.

Page 46: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• Excel will likely display a warning box that information may be lost by saving in a text file, but you must use a text file for LabVIEW. The numeric values will not be lost when the file is saved as a text file.

Note: Undisplayed digits are lost when the text file is saved. The Time Temperature.txt file will contain values with only one decimal place (because that’s how they were displayed in the Excel worksheet, see Figure 4.18).

Step 3: Place a Read From Spreadsheet File function on a LabVIEW block diagram

• The Read From Spreadsheet File function is in the Programming Group’s File I/O Group on the Functions Palette.

Functions Palette / Programming Group / File I/O Group / Read From Spreadsheet File

Page 47: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• The annotated connection pane for the function is illustrated in Figure 4.21.

• Most of the terminals have default values that work most of the time.

Figure 4.21. Connection pane for the Read From Spreadsheet

File function.

Page 48: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Inputs: • format (%.3f)—the format string used to read the values from the text file.

• file path (dialog if empty)—this is a path variable indicating where the file

should be stored. If no path is specified, the user is asked to choose a file at

run time.

• number of rows (all:1)—if only a part of the file is to be read, you must

indicate the number of rows to be read. Accept the default to read the

entire file.

• start of read offset (chars.: 0)—if the top-left corner of the data set is not

in cell A1, you must indicate the required offset. Because the offset must

be specified in characters, not cells, this is very inconvenient to use.

• max characters/row (no limit: 0)—you can set a limit on the number of

characters read per row.The default is to read the entire row.

• transpose? (no: F)—a Boolean (True or False) value indicating whether

or not the data should be transposed (rows and columns interchanged) as

it is read.The default is not to transpose the data.

• delimiter (\t)—the delimiter is the value that has been placed between

numbers to separate them.The default is a tab character (“\t”).

Page 49: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Outputs:

• new file path—provides programmatic access to the file path that the user may have selected using the Choose File to Write dialog.

• all rows—this is the most commonly used output terminal. All rows of data are available through this output.

• first row—this terminal provides access to only the first row of values.

• mark after read (chars.)—the location of the file marker after the file has been read. Rarely used, this might be useful if you needed to append data to the end of the file.

• EOF?—this output is set to TRUE when the end of the file has been read.

In this example, we will set a format string constant to increase the number of decimal places slightly, and send all rows to an indicator.

Page 50: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Step 4: Select the data type of the values to be read from the file

• LabVIEW must know the data type of the values that will be read so that it can read, store, and display the values correctly. The default data type is “Double” (double-precision floating point numbers), as indicated on the data type selector shown below the Read From Spreadsheet File function icon in Figure 4.22. In this example, we want to use the Double data type.

Note: The data type selector can be hidden. Right-click on the node and select Visible Items from the pop-up menu if needed.

Figure 4.22

The drop-

down

selector

labeled

“Double” is

used

to indicate

the data

type

of the values

to be read.

Page 51: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Step 5: Use a string constant to specify the format string

• To attempt to display more decimal places than were saved in the text file, we will instruct LabVIEW to use format string “%.5f” to read the data. This will allow us to observe how LabVIEW handles a request to read more digits than are present in the values in the file.

• To set the format string:

1. Right-click on the format input terminal.

2. Select Create/Constant from the pop-up menu.

3. Enter “%.5f” (without the quotes) into the constant.

• The result is shown in Figure 6.24.

Figure 4.22

Setting the

string

constant

that

specifies the

read format.

Page 52: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Step 6: Use a string constant to specify the path

name

• If the path name is not specified, LabVIEW will ask the

user to choose the file at run time. In this example, we

will not specify the file path name.

Step 7: Add an indicator to the block diagram to

display the read values

• Right-click on the Read From Spreadsheet File

function’s all rows output and select Create / Indicator

from the pop-up menu. LabVIEW will add an array

indicator to the block diagram and the front panel to

display the values read from the file. The complete (for

now) block diagram is shown in Figure 4.23.

Page 53: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• When the VI is run from the front panel, the user is asked to select the file to be read as shown in Figure 4.24.

• The results are shown in Figure 4.25. The array indicator was resized to show the entire array of values.

• Recall that we asked LabVIEW to read five decimal places (“%.5f”), but the values in the text file contained only one decimal place. LabVIEW read the values in the file anyway, with as much precision as the values would allow. The values are displayed on the front panel with three decimal places because of the (default) display format used with the array indicator.

Figure 4.23

Block diagram, ready to read

data from text file.

Page 54: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Figure 4.24. The Choose file to read dialog is used to select the

text file.

Page 55: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Figure 4.25. Front panel showing the values read from the text file.

Page 56: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Understanding Low-Level File I/O • Low-level file I/O VIs and functions each perform only

one piece of the file I/O process. For example, there is one function to open an ASCII file, one function to read an ASCII file, and one function to close an ASCII file. Use low-level functions when file I/O is occurring within a loop.

• You also can use File I/O functions for disk streaming operations, which save memory resources by reducing the number of times the function interacts with the operating system to open and close the file.

• Disk streaming is a technique for keeping files open while you perform multiple write operations, for example, within a loop.

• The examples in Figure 4.26 and Figure 4.27 show the advantages of using disk streaming.

Page 57: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• In Figure 4.26, the VI must open and close the file during each iteration of the loop. Figure 4.27 uses disk streaming to reduce the number of times the VI must interact with the operating system to open and close the file.

Figure 4.26. Non-Disk Streaming Example

Page 58: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

• By opening the file once before the loop begins and closing it after the loop completes, you save two file operations on each iteration of the loop.

Figure 4.27. Disk Streaming Example

Page 59: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Model de program pentru un Fișier I/O

Deschide/

Crează/

Înlocuiește

Fișier

Citește

și/sau

Scrie în Fișier

Închide

Fișier

Verifică

Erorile

Page 60: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Salvarea (Scrierea) datelor într-un fișier

• Deschide/Crează/Înlocuiește, deschide fișierul existent test1.dat și generează fișierul identificator refnum și clusterul error, care este un mănunchi de date ce conține informații referitoare la erorile posibile.

• Write File scrie datele • Close File închide fișierul • Simple Error Handler verifică erorile

Page 61: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Exemplu de scriere într-un fișier

• Open/Create/Replace opens the existing file TEST1.DAT and

generates refnum and error cluster

• Write File writes the data

• Close File closes the file

• Simple Error Handler checks for errors

Page 62: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Citirea datelor dintr-un fișier

• Open/Create/Replace opens the file

• Read File reads the specified number of bytes from the file

• Close File closes the file

• Simple Error Handler checks for errors

Page 63: 9. Stringuri și Fișiere I/O (Intrare/Ieșire) · 2019. 5. 6. · String/char în textul conectat la intrarea String. Căutarea se efectuează începând cu poziţia Offset. Dacă

Exercițiu: Să se realizeze VI-ul de mai jos, care stochează datele

într-un fișier și care afișază valoarea medie, maximă și minimă a

acestor date